Advertisement
uopspop

Untitled

Aug 23rd, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. package com.frankmoley.security.config;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
  6. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  7. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  8. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  9. import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
  10. import org.springframework.security.crypto.password.LdapShaPasswordEncoder;
  11. import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
  12. import org.springframework.stereotype.Component;
  13.  
  14. @Component
  15. public class MyWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter{
  16.    
  17.     private GrantedAuthoritiesMapper grantedAuthoritiesMapper;
  18.    
  19.     @Autowired
  20.     public MyWebSecurityConfigurerAdapter(GrantedAuthoritiesMapper grantedAuthoritiesMapper) {
  21.         super();
  22.         this.grantedAuthoritiesMapper = grantedAuthoritiesMapper;
  23.     }
  24.  
  25.     @Override
  26.     protected void configure(HttpSecurity http) throws Exception {
  27.         // super.configure(http);
  28.         http.csrf().disable()
  29.             .authorizeRequests()
  30.             .antMatchers("/","/index","/css/*","/js/*").permitAll()
  31.             .anyRequest().authenticated()
  32.             .and()
  33.             .formLogin()
  34.             .loginPage("/login_page").permitAll()
  35.             .loginProcessingUrl("/login").permitAll()
  36.             .defaultSuccessUrl("/index", true)
  37.             .and()
  38.             .logout().invalidateHttpSession(true)
  39.             .clearAuthentication(true)
  40.             .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
  41.             .logoutSuccessUrl("/logout_success_page").permitAll()
  42.             ;
  43.     }
  44.    
  45.     @Override
  46.     protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  47.         auth.ldapAuthentication()
  48.             .userDnPatterns("uid={0},ou=people")
  49.             .groupSearchBase("ou=groups")
  50.             .authoritiesMapper(this.grantedAuthoritiesMapper)
  51.             .contextSource().url("ldap://localhost:8389/dc=frankmoley,dc=com")
  52.             .and()
  53.             .passwordCompare()
  54.             .passwordEncoder(new LdapShaPasswordEncoder()) // use other encoder like BCryptEncoder for production environment instead
  55.             .passwordAttribute("userPassword");
  56.     }
  57.    
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement