WebSecurityConfig.java
2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.tianbo.warehouse.security;
import org.springframework.beans.factory.annotation.Autowired;
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;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private MyFilterSecurityInterceptor myFilterSecurityInterceptor;
@Autowired
private PasswordEncoder passwordEncoder;
@Qualifier("customuserservice")
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//user Details Service验证
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin","/role").authenticated()
//管理页面只允许管理员角色访问 //任何请求,登录后可以访问
.anyRequest().permitAll() //其余的不需要验证
.and()
.formLogin()
.passwordParameter("password")
.usernameParameter("username")
//.loginProcessingUrl("/home")//登陆提交的处理url
.loginPage("/login")
.failureUrl("/error")
.permitAll()//登录页面用户任意访问
.successForwardUrl("/main")
.and()
.logout()
.logoutSuccessUrl("/?logout=true")
.permitAll()
.and()
.rememberMe()
.tokenValiditySeconds(604800)
//记住我功能,cookies有限期是一周
.rememberMeParameter("remeberme")
//登陆时是否激活记住我功能的参数名字,在登陆页面有展示
.rememberMeCookieName("workspace")
//cookies的名字,登陆后可以通过浏览器查看cookies名字
.and()
.cors()
.and()
.csrf().disable();
//http.addFilterBefore(myFilterSecurityInterceptor, FilterSecurityInterceptor.class).csrf().disable();
}
}