- Notifications
You must be signed in to change notification settings - Fork 769
Expand file tree
/
Copy pathWebSecurityConfig.java
More file actions
Latest commit
118 lines (95 loc) · 4.8 KB
/
Copy pathWebSecurityConfig.java
File metadata and controls
118 lines (95 loc) · 4.8 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
packageorg.joychou.security;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Value;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
importorg.springframework.security.config.annotation.web.builders.HttpSecurity;
importorg.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
importorg.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
importorg.springframework.security.web.csrf.CookieCsrfTokenRepository;
importorg.springframework.security.web.util.matcher.RequestMatcher;
importorg.springframework.web.cors.CorsConfiguration;
importorg.springframework.web.cors.CorsConfigurationSource;
importorg.springframework.web.cors.UrlBasedCorsConfigurationSource;
importjavax.servlet.http.HttpServletRequest;
importjava.util.ArrayList;
importjava.util.Arrays;
importjava.util.HashSet;
/**
* Congifure csrf
*
*/
@EnableWebSecurity
@Configuration
publicclassWebSecurityConfigextendsWebSecurityConfigurerAdapter {
@Value("${joychou.security.csrf.enabled}")
privateBooleancsrfEnabled = false;
@Value("${joychou.security.csrf.exclude.url}")
privateString[] csrfExcludeUrl;
@Value("${joychou.no.need.login.url}")
privateString[] noNeedLoginUrl;
@Value("${joychou.security.csrf.method}")
privateString[] csrfMethod = {"POST"};
privateRequestMatchercsrfRequestMatcher = newRequestMatcher() {
@Override
publicbooleanmatches(HttpServletRequestrequest) {
// 配置需要CSRF校验的请求方式,
HashSet<String> allowedMethods = newHashSet<>(Arrays.asList(csrfMethod));
// return false表示不校验csrf
if (!csrfEnabled) {
returnfalse;
}
returnallowedMethods.contains(request.getMethod());
}
};
@Override
protectedvoidconfigure(HttpSecurityhttp) throwsException {
// 默认token存在session里,用CookieCsrfTokenRepository改为token存在cookie里。
// 但存在后端多台服务器情况,session不能同步的问题,所以一般使用cookie模式。
http.csrf()
.requireCsrfProtectionMatcher(csrfRequestMatcher)
.ignoringAntMatchers(csrfExcludeUrl) // 不进行csrf校验的uri,多个uri使用逗号分隔
.csrfTokenRepository(newCookieCsrfTokenRepository());
http.exceptionHandling().accessDeniedHandler(newCsrfAccessDeniedHandler());
// http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
http.cors();
// spring security login settings
http.authorizeRequests()
.antMatchers(noNeedLoginUrl).permitAll() // no need to login page
// CVE-2022-22978漏洞代码
.regexMatchers("/black_path.*").denyAll() // 如果正则匹配到/black_path,则forbidden
.anyRequest().authenticated().and() // any request authenticated except above static resources
.formLogin().loginPage("/login").permitAll() // permit all to access /login page
.successHandler(newLoginSuccessHandler())
.failureHandler(newLoginFailureHandler()).and()
.logout().logoutUrl("/logout").permitAll().and()
// tomcat默认JSESSION会话有效时间为30分钟,所以30分钟不操作会话将过期。为了解决这一问题,引入rememberMe功能。
.rememberMe();
}
/**
* Global cors configure
*/
@Bean
CorsConfigurationSourcecorsConfigurationSource()
{
// Set cors origin white list
ArrayList<String> allowOrigins = newArrayList<>();
allowOrigins.add("joychou.org");
allowOrigins.add("https://test.joychou.me"); // 区分http和https,并且默认不会拦截同域请求。
CorsConfigurationconfiguration = newCorsConfiguration();
configuration.setAllowedOrigins(allowOrigins);
configuration.setAllowCredentials(true);
configuration.setAllowedMethods(Arrays.asList("GET", "POST"));
UrlBasedCorsConfigurationSourcesource = newUrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/cors/sec/httpCors", configuration); // ant style
returnsource;
}
@Autowired
publicvoidconfigureGlobal(AuthenticationManagerBuilderauth) throwsException {
auth
.inMemoryAuthentication()
.withUser("joychou").password("joychou123").roles("USER").and()
.withUser("admin").password("admin123").roles("USER", "ADMIN");
}
}