springboot框架实现登录注册,登出,图形验证码功能
时间: 2024-05-06 18:14:58 浏览: 10
可以通过Spring Security来实现登录注册和登出功能,同时使用第三方库生成图形验证码。
1. 登录注册
在Spring Boot中使用Spring Security实现登录注册功能,需要引入以下依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
```
然后创建一个SecurityConfig类来配置Spring Security,示例代码如下:
```java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/registration").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
```
其中,configure(AuthenticationManagerBuilder auth)方法用于配置用户认证方式,这里使用了UserDetailsService来获取用户信息,同时使用BCryptPasswordEncoder进行密码加密;configure(HttpSecurity http)方法用于配置请求授权和登录等信息。
2. 图形验证码
可以使用第三方库Kaptcha来生成图形验证码,需要引入以下依赖:
```xml
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
```
然后创建一个KaptchaConfig类来配置Kaptcha,示例代码如下:
```java
@Configuration
public class KaptchaConfig {
@Bean
public Producer captcha() {
Properties properties = new Properties();
properties.put("kaptcha.border", "no");
properties.put("kaptcha.textproducer.font.color", "black");
properties.put("kaptcha.image.width", "150");
properties.put("kaptcha.image.height", "50");
properties.put("kaptcha.textproducer.char.length", "4");
Config config = new Config(properties);
DefaultKaptcha kaptcha = new DefaultKaptcha();
kaptcha.setConfig(config);
return kaptcha;
}
}
```
在Controller中使用Kaptcha生成图形验证码,示例代码如下:
```java
@RestController
public class CaptchaController {
@Autowired
private Producer captchaProducer;
@GetMapping("/captcha")
public void getCaptcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
String capText = captchaProducer.createText();
request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
BufferedImage bi = captchaProducer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
}
}
```
在前端页面中使用img标签来显示图形验证码,示例代码如下:
```html
<img src="/captcha" alt="Captcha" />
```
3. 登出
在SecurityConfig中配置logout()方法即可实现登出功能,示例代码如下:
```java
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/registration").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
```
在前端页面中使用a标签访问/logout路径即可实现登出,示例代码如下:
```html
<a href="/logout">Logout</a>
```