Skip to content

Instantly share code, notes, and snippets.

@BrunoDSouza
Last active September 19, 2019 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrunoDSouza/39d4341fec4f6cc7144ee5cd5c669089 to your computer and use it in GitHub Desktop.
Save BrunoDSouza/39d4341fec4f6cc7144ee5cd5c669089 to your computer and use it in GitHub Desktop.
Spring Security Settings
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);
}
}
@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;
}
};
}
}
@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