Skip to content

Commit 8bc4368

Browse files
committed
登录加入验证码校验
1 parent 57f9999 commit 8bc4368

File tree

6 files changed

+143
-7
lines changed

6 files changed

+143
-7
lines changed

multi-user-table-mybatis/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@
6565
<artifactId>spring-security-test</artifactId>
6666
<scope>test</scope>
6767
</dependency>
68+
<!-- 验证码依赖-->
69+
<dependency>
70+
<groupId>com.github.penggle</groupId>
71+
<artifactId>kaptcha</artifactId>
72+
<version>2.3.2</version>
73+
</dependency>
6874
</dependencies>
6975

7076
<build>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.sspath.multiusertablemybatis.config;
2+
3+
import org.springframework.security.authentication.AuthenticationServiceException;
4+
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
5+
import org.springframework.security.core.Authentication;
6+
import org.springframework.security.core.AuthenticationException;
7+
import org.springframework.web.context.request.RequestContextHolder;
8+
import org.springframework.web.context.request.ServletRequestAttributes;
9+
10+
import javax.servlet.http.HttpServletRequest;
11+
12+
/**
13+
* @FileName: KaptchaAuthenticationProvider.java
14+
* @Description: 重写authenticate方法,在数据库查询用户名密码前,做验证码校验
15+
* 重写DaoAuthenticationProvider的additionalAuthenticationChecks方法,是在数据库查询后检测验证码,不符合验证码初衷
16+
* @Author: ABCpril
17+
* @Date: 2021/11/11
18+
*/
19+
public class KaptchaAuthenticationProvider extends DaoAuthenticationProvider {
20+
@Override
21+
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
22+
HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
23+
String kaptcha = req.getParameter("kaptcha");
24+
String sessionKaptcha = (String) req.getSession().getAttribute("kaptcha");
25+
if (kaptcha != null && sessionKaptcha != null && kaptcha.equalsIgnoreCase(sessionKaptcha)) {
26+
return super.authenticate(authentication);
27+
}
28+
throw new AuthenticationServiceException("验证码输入错误");
29+
}
30+
31+
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.sspath.multiusertablemybatis.config;
2+
3+
import com.google.code.kaptcha.Producer;
4+
import com.google.code.kaptcha.impl.DefaultKaptcha;
5+
import com.google.code.kaptcha.util.Config;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
9+
import java.util.Properties;
10+
11+
/**
12+
* @FileName: KaptchaConfig.java
13+
* @Description: 验证码配置类
14+
* @Author: ABCpril
15+
* @Date: 2021/11/11
16+
*/
17+
18+
@Configuration
19+
public class KaptchaConfig {
20+
@Bean
21+
Producer kaptcha() {
22+
Properties properties = new Properties();
23+
properties.setProperty("kaptcha.image.width", "150");
24+
properties.setProperty("kaptcha.image.height", "50");
25+
properties.setProperty("kaptcha.textproducer.cha.string", "0123456789");
26+
properties.setProperty("kaptcha.textproducer.char.length", "4");
27+
Config config = new Config(properties);
28+
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
29+
defaultKaptcha.setConfig(config);
30+
return defaultKaptcha;
31+
}
32+
33+
}

multi-user-table-mybatis/src/main/java/com/sspath/multiusertablemybatis/config/SecurityConfig.java

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.context.annotation.Bean;
77
import org.springframework.context.annotation.Configuration;
88
import org.springframework.security.authentication.AuthenticationManager;
9+
import org.springframework.security.authentication.AuthenticationProvider;
910
import org.springframework.security.authentication.ProviderManager;
1011
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
1112
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@@ -25,22 +26,44 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
2526
@Autowired
2627
MyUserDetailsService2 myUserDetailsService2;
2728

28-
@Override
29+
// @Override
30+
// @Bean
31+
// public AuthenticationManager authenticationManagerBean() throws Exception {
32+
// DaoAuthenticationProvider dao1 = new DaoAuthenticationProvider();
33+
// dao1.setUserDetailsService(myUserDetailsService);
34+
//
35+
// DaoAuthenticationProvider dao2 = new DaoAuthenticationProvider();
36+
// dao2.setUserDetailsService(myUserDetailsService2);
37+
//
38+
// ProviderManager manager = new ProviderManager(dao1, dao2);
39+
// return manager;
40+
// }
41+
2942
@Bean
30-
public AuthenticationManager authenticationManagerBean() throws Exception {
31-
DaoAuthenticationProvider dao1 = new DaoAuthenticationProvider();
32-
dao1.setUserDetailsService(myUserDetailsService);
43+
AuthenticationProvider kaptchaAuthenticationProvider() {
44+
KaptchaAuthenticationProvider provider = new KaptchaAuthenticationProvider();
45+
provider.setUserDetailsService(myUserDetailsService);
46+
return provider;
47+
}
3348

34-
DaoAuthenticationProvider dao2 = new DaoAuthenticationProvider();
35-
dao2.setUserDetailsService(myUserDetailsService2);
49+
@Bean
50+
AuthenticationProvider kaptchaAuthenticationProvider2() {
51+
KaptchaAuthenticationProvider provider2 = new KaptchaAuthenticationProvider();
52+
provider2.setUserDetailsService(myUserDetailsService2);
53+
return provider2;
54+
}
3655

37-
ProviderManager manager = new ProviderManager(dao1, dao2);
56+
@Override
57+
@Bean
58+
public AuthenticationManager authenticationManagerBean() throws Exception {
59+
ProviderManager manager = new ProviderManager(kaptchaAuthenticationProvider(), kaptchaAuthenticationProvider2());
3860
return manager;
3961
}
4062

4163
@Override
4264
protected void configure(HttpSecurity http) throws Exception {
4365
http.authorizeRequests()
66+
.antMatchers("/vc.jpg").permitAll()
4467
.anyRequest().authenticated()
4568
.and()
4669
.formLogin()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.sspath.multiusertablemybatis.controller;
2+
3+
import com.google.code.kaptcha.Producer;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
import javax.imageio.ImageIO;
9+
import javax.servlet.ServletOutputStream;
10+
import javax.servlet.http.HttpServletResponse;
11+
import javax.servlet.http.HttpSession;
12+
import java.awt.image.BufferedImage;
13+
import java.io.IOException;
14+
15+
/**
16+
* @FileName: KaptchaController.java
17+
* @Description: 验证码请求接口,验证码文本写入session,验证码图片通过IO流写出到前端
18+
* @Author: ABCpril
19+
* @Date: 2021/11/11
20+
*/
21+
22+
@RestController
23+
public class KaptchaController {
24+
@Autowired
25+
Producer producer;
26+
27+
@GetMapping("/vc.jpg")
28+
public void getVerifyCode(HttpServletResponse resp, HttpSession session) throws IOException {
29+
resp.setContentType("image/jpeg");
30+
String text = producer.createText();
31+
session.setAttribute("kaptcha", text);
32+
BufferedImage image = producer.createImage(text);
33+
try(ServletOutputStream out = resp.getOutputStream()) {
34+
ImageIO.write(image, "jpg", out);
35+
}
36+
}
37+
}

multi-user-table-mybatis/src/main/resources/templates/mylogin.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ <h3 class="text-center text-info">登录</h3>
2929
<label for="password" class="text-info">密码:</label><br>
3030
<input type="text" name="passwd" id="password" class="form-control">
3131
</div>
32+
<div class="form-group">
33+
<label for="kaptcha" class="text-info">验证码:</label><br>
34+
<input type="text" name="kaptcha" id="kaptcha" class="form-control">
35+
<img src="/vc.jpg" alt="">
36+
</div>
3237
<div class="form-group">
3338
<input type="submit" name="submit" class="btn btn-info btn-md" value="登录">
3439
</div>

0 commit comments

Comments
 (0)