Last active
September 19, 2019 16:56
-
-
Save BrunoDSouza/39d4341fec4f6cc7144ee5cd5c669089 to your computer and use it in GitHub Desktop.
Spring Security Settings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AuthenticationManagerCustom implements AuthenticationManager{ | |
@Autowired | |
private UserService userService; | |
@Override | |
public Authentication authenticate(Authentication auth) throws AuthenticationException { | |
String username = auth.getName(); | |
String password = auth.getCredentials().toString(); | |
Authentication authorities = userService.getUserDetailsAuthentication(); | |
Collection<? extends GrantedAuthority> grantedAuths = authorities.getAuthorities(); | |
return new UsernamePasswordAuthenticationToken(username, password, grantedAuths); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@EnableGlobalMethodSecurity(prePostEnabled = true) | |
@EnableWebSecurity | |
@EnableJpaRepositories(basePackageClasses = UsersRepository.class) | |
@Configuration | |
public class SecurityConfig extends WebSecurityConfigurerAdapter { | |
@Autowired private UserService userDetailsService; | |
@Autowired | |
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{ | |
auth.userDetailsService(userDetailsService) | |
.passwordEncoder(passwordEnconder()); | |
} | |
@Override | |
protected void configure(HttpSecurity http) throws Exception{ | |
http.csrf().disable() | |
.authorizeRequests() | |
.antMatchers("/produto").hasAnyRole(Acls.FUNC, Acls.ADMIN) | |
.antMatchers("/fornecedor").hasAnyRole(Acls.FUNC, Acls.ADMIN) | |
.antMatchers("/tipos").hasAnyRole(Acls.FUNC, Acls.ADMIN) | |
.antMatchers("/movimentacao").hasAnyRole(Acls.FUNC, Acls.ADMIN) | |
.antMatchers("/setor").hasAnyRole(Acls.FUNC, Acls.ADMIN) | |
.antMatchers("/produto/**").hasRole(Acls.ADMIN) | |
.antMatchers("/fornecedor/**").hasRole(Acls.ADMIN) | |
.antMatchers("/tipos/**").hasRole(Acls.ADMIN) | |
.antMatchers("/setor/**").hasRole(Acls.ADMIN) | |
.anyRequest().authenticated() | |
.and() | |
.formLogin() | |
.loginPage("/").permitAll() | |
.failureUrl("/login/?error=true").permitAll() | |
.defaultSuccessUrl("/home").permitAll() | |
.loginPage("/").permitAll() | |
.and() | |
.logout() | |
.logoutRequestMatcher(new AntPathRequestMatcher("/logout")) | |
.logoutSuccessUrl("/login/?logout").permitAll() | |
.invalidateHttpSession(true).permitAll() | |
.clearAuthentication(true) | |
.deleteCookies("SPRING_SECURITY_REMEMBER_MECOOKIE","JSESSIONID"); | |
} | |
@Bean(name = BeanIds.AUTHENTICATION_MANAGER) | |
@Override | |
public AuthenticationManager authenticationManagerBean() throws Exception { | |
return super.authenticationManagerBean(); | |
} | |
@Override | |
public void configure(WebSecurity web) throws Exception{ | |
web.ignoring() | |
.antMatchers("/layout/**", | |
"/stylesheets/**", | |
"/javascripts/**", | |
"/images/**"); | |
} | |
@Bean | |
public BCryptPasswordEncoder passwordEnconder(){ | |
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); | |
return bCryptPasswordEncoder; | |
} | |
@Bean | |
public PasswordEncoder getPasswordEncoder() { | |
return new PasswordEncoder() { | |
@Override | |
public String encode(CharSequence charSequence) { | |
return charSequence.toString(); | |
} | |
@Override | |
public boolean matches(CharSequence charSequence, String s) { | |
return true; | |
} | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@EnableSpringConfigured | |
public class WebConfig extends WebMvcConfigurerAdapter { | |
public WebConfig() { | |
super(); | |
} | |
@Bean | |
public BCryptPasswordEncoder passwordEnconder(){ | |
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); | |
return bCryptPasswordEncoder; | |
} | |
@Bean | |
public LocaleResolver localeResolver(){ | |
return new FixedLocaleResolver(new Locale("pt", "BR")); | |
} | |
@Bean(name = "AuthCustom") | |
public AuthenticationManagerCustom getAuthenticationCustom(){ | |
return new AuthenticationManagerCustom(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment