| 
 | 
 | 
 package com.tianbo.warehouse.security;
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 import org.springframework.beans.factory.annotation.Autowired;
 | 
| 
 | 
 | 
 import org.springframework.context.annotation.Bean;
 | 
| 
 | 
 | 
 import org.springframework.beans.factory.annotation.Qualifier;
 | 
| 
 | 
 | 
 import org.springframework.context.annotation.Configuration;
 | 
| 
 | 
 | 
 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
 | 
| 
 | 
 | 
 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
 | 
| 
...
 | 
...
 | 
@@ -21,29 +21,48 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { | 
| 
 | 
 | 
     @Autowired
 | 
| 
 | 
 | 
     private  PasswordEncoder passwordEncoder;
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     @Bean
 | 
| 
 | 
 | 
     UserDetailsService customUserService(){ //注册UserDetailsService 的bean
 | 
| 
 | 
 | 
         return new CustomUserDetailService();
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
     @Qualifier("customuserservice")
 | 
| 
 | 
 | 
     @Autowired
 | 
| 
 | 
 | 
     private  UserDetailsService userDetailsService;
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     @Override
 | 
| 
 | 
 | 
     protected void configure(AuthenticationManagerBuilder  auth) throws Exception {
 | 
| 
 | 
 | 
         //user Details Service验证
 | 
| 
 | 
 | 
         auth.userDetailsService(customUserService()).passwordEncoder(passwordEncoder);
 | 
| 
 | 
 | 
         auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     @Override
 | 
| 
 | 
 | 
     protected void configure(HttpSecurity http) throws Exception {
 | 
| 
 | 
 | 
         http.authorizeRequests()
 | 
| 
 | 
 | 
                 .anyRequest().authenticated() //任何请求,登录后可以访问
 | 
| 
 | 
 | 
                 .antMatchers("/admin","/role").authenticated()
 | 
| 
 | 
 | 
                 //管理页面只允许管理员角色访问  //任何请求,登录后可以访问
 | 
| 
 | 
 | 
                 .anyRequest().permitAll() //其余的不需要验证
 | 
| 
 | 
 | 
                 .and()
 | 
| 
 | 
 | 
                 .formLogin()
 | 
| 
 | 
 | 
                 .loginProcessingUrl("/home")
 | 
| 
 | 
 | 
                 .passwordParameter("password")
 | 
| 
 | 
 | 
                 .usernameParameter("username")
 | 
| 
 | 
 | 
                 //.loginProcessingUrl("/home")//登陆提交的处理url
 | 
| 
 | 
 | 
                 .loginPage("/login")
 | 
| 
 | 
 | 
                 .failureUrl("/login?error")
 | 
| 
 | 
 | 
                 .permitAll() //登录页面用户任意访问
 | 
| 
 | 
 | 
                 .failureUrl("/error")
 | 
| 
 | 
 | 
                 .permitAll()//登录页面用户任意访问
 | 
| 
 | 
 | 
                 .successForwardUrl("/main")
 | 
| 
 | 
 | 
                 .and()
 | 
| 
 | 
 | 
                 .logout()
 | 
| 
 | 
 | 
                 .logoutSuccessUrl("/?logout=true")
 | 
| 
 | 
 | 
                 .permitAll()
 | 
| 
 | 
 | 
                 .and()
 | 
| 
 | 
 | 
                 .logout().permitAll(); //注销行为任意访问
 | 
| 
 | 
 | 
         http.addFilterBefore(myFilterSecurityInterceptor, FilterSecurityInterceptor.class);
 | 
| 
 | 
 | 
                 .rememberMe()
 | 
| 
 | 
 | 
                 .tokenValiditySeconds(604800)
 | 
| 
 | 
 | 
                 //记住我功能,cookies有限期是一周
 | 
| 
 | 
 | 
                 .rememberMeParameter("remeberme")
 | 
| 
 | 
 | 
                 //登陆时是否激活记住我功能的参数名字,在登陆页面有展示
 | 
| 
 | 
 | 
                 .rememberMeCookieName("workspace")
 | 
| 
 | 
 | 
                 //cookies的名字,登陆后可以通过浏览器查看cookies名字
 | 
| 
 | 
 | 
                 .and()
 | 
| 
 | 
 | 
                 .cors()
 | 
| 
 | 
 | 
                 .and()
 | 
| 
 | 
 | 
                 .csrf().disable();
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         //http.addFilterBefore(myFilterSecurityInterceptor, FilterSecurityInterceptor.class).csrf().disable();
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
 } | 
...
 | 
...
 | 
 |