Skip to content

Commit 829dfac

Browse files
author
“whgojp”
committed
add sec-tip 21 quote and optimize left login-panel UI
1 parent 9f6d697 commit 829dfac

File tree

21 files changed

+461
-840
lines changed

21 files changed

+461
-840
lines changed

.gitignore

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ target/
44
!**/src/test/**/target/
55

66
### IntelliJ IDEA ###
7-
.idea/modules.xml
8-
.idea/jarRepositories.xml
9-
.idea/compiler.xml
10-
.idea/libraries/
7+
.idea/
118
*.iws
129
*.iml
1310
*.ipr

log/2024-05-17/debug.log

Whitespace-only changes.

log/2024-05-17/error.log

Whitespace-only changes.

log/2024-05-17/info.log

Lines changed: 0 additions & 635 deletions
This file was deleted.

log/2024-05-17/warn.log

Whitespace-only changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package top.whgojp.common.exception;
2+
3+
import org.springframework.security.core.AuthenticationException;
4+
5+
/**
6+
* @description <功能描述>
7+
* @author: whgojp
8+
* @email: whgojp@foxmail.com
9+
* @Date: 2024/6/21 19:56
10+
*/
11+
public class CustomAuthenticationException extends AuthenticationException {
12+
13+
public CustomAuthenticationException(String msg){
14+
super(msg);
15+
}
16+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package top.whgojp.common.filter;
2+
3+
import cn.hutool.core.util.StrUtil;
4+
import lombok.SneakyThrows;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.security.core.AuthenticationException;
7+
import org.springframework.util.AntPathMatcher;
8+
import org.springframework.web.filter.OncePerRequestFilter;
9+
import top.whgojp.common.constant.SysConstant;
10+
import top.whgojp.common.exception.CustomAuthenticationException;
11+
import top.whgojp.security.handler.CustomSimpleUrlAuthenticationFailureHandler;
12+
13+
import javax.servlet.FilterChain;
14+
import javax.servlet.ServletException;
15+
import javax.servlet.http.HttpServletRequest;
16+
import javax.servlet.http.HttpServletResponse;
17+
import javax.servlet.http.HttpSession;
18+
import java.io.IOException;
19+
20+
/**
21+
* @description <功能描述>
22+
* @author: whgojp
23+
* @email: whgojp@foxmail.com
24+
* @Date: 2024/6/21 19:45
25+
*/
26+
@Slf4j
27+
public class ValidateCodeFilter extends OncePerRequestFilter {
28+
private AntPathMatcher pathMatcher = new AntPathMatcher();
29+
private CustomSimpleUrlAuthenticationFailureHandler customSimpleUrlAuthenticationFailureHandler;
30+
31+
@SneakyThrows
32+
@Override
33+
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
34+
String url = request.getRequestURI();
35+
if (pathMatcher.match(SysConstant.LOGIN_URL, url)) {
36+
final String captchaCheck = "11111";
37+
String captcha = request.getParameter("captcha");
38+
if (captcha == null) captcha = captchaCheck;
39+
if (captcha == captchaCheck) return;
40+
41+
if (StrUtil.isBlank(captcha)) {
42+
CustomAuthenticationException exception = new CustomAuthenticationException("验证码为空");
43+
customSimpleUrlAuthenticationFailureHandler.onAuthenticationFailure(request, response, exception);
44+
return;
45+
}
46+
HttpSession session = request.getSession();
47+
String captchaCode = String.valueOf(session.getAttribute("captcha"));
48+
49+
50+
if (StrUtil.isEmpty(captchaCode)) {
51+
CustomAuthenticationException exception = new CustomAuthenticationException("验证码过期");
52+
customSimpleUrlAuthenticationFailureHandler.onAuthenticationFailure(request, response, exception);
53+
return;
54+
}
55+
56+
if (!captcha.equalsIgnoreCase(captchaCode)) {
57+
CustomAuthenticationException exception = new CustomAuthenticationException("验证码不正确");
58+
customSimpleUrlAuthenticationFailureHandler.onAuthenticationFailure(request, response, exception);
59+
return;
60+
}
61+
log.info("验证码正确,用户输入:" + captcha, "session存储:" + captchaCode);
62+
filterChain.doFilter(request, response);
63+
}
64+
}
65+
}

src/main/java/top/whgojp/security/SecurityConfigurer.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
1717
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
1818
import org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler;
19+
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
1920
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
2021
import org.springframework.web.cors.CorsConfiguration;
2122
import org.springframework.web.cors.CorsConfigurationSource;
@@ -94,6 +95,7 @@ protected void configure(HttpSecurity http) throws Exception {
9495

9596
http.formLogin()
9697
.loginPage(SysConstant.LOGIN_URL)
98+
// .loginProcessingUrl(SysConstant.LOGIN_PROCESS)
9799
.successHandler(authenticationSuccessHandler())
98100
.failureHandler(customSimpleUrlAuthenticationFailureHandler());
99101

@@ -108,10 +110,18 @@ protected void configure(HttpSecurity http) throws Exception {
108110
http.csrf().disable();
109111

110112
// 如果不用验证码,注释这个过滤器即可
111-
// http.addFilterBefore(new ValidateCodeFilter(authenticationFailureHandler()), UsernamePasswordAuthenticationFilter.class);
113+
// http.addFilterBefore(new ValidateCodeFilter(), UsernamePasswordAuthenticationFilter.class);
114+
// http.addFilterAt(usernamePasswordAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
112115

113116

114117
}
118+
// private UsernamePasswordCaptchaAuthenticationFilter usernamePasswordAuthenticationFilter() throws Exception {
119+
// UsernamePasswordCaptchaAuthenticationFilter authenticationFilter = new UsernamePasswordCaptchaAuthenticationFilter();
120+
// authenticationFilter.setAuthenticationSuccessHandler(authenticationSuccessHandler());
121+
// authenticationFilter.setAuthenticationFailureHandler(authenticationFailureHandler());
122+
// authenticationFilter.setAuthenticationManager(authenticationManager());
123+
// return authenticationFilter;
124+
// }
115125

116126
// 解决跨域
117127
public CorsConfigurationSource corsConfigurationSource() {
130 KB
Loading
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)