添加验证码校验
This commit is contained in:
+1
-1
@@ -61,7 +61,7 @@ public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
.failureHandler(msAuthenticationFailureHandler)//定义身份认证失败后处理对象
|
||||
.and()
|
||||
.authorizeRequests()//下面是权限配置
|
||||
.antMatchers("/authentication/require",securityProperties.getBrowser().getHtml()).permitAll()//当访问这个页面的时候不进行身份认证,对用户自定义的登录页也不进行权限认证
|
||||
.antMatchers("/code/image","/authentication/require",securityProperties.getBrowser().getHtml()).permitAll()//当访问这个页面的时候不进行身份认证,对用户自定义的登录页也不进行权限认证
|
||||
.anyRequest()//任何请求
|
||||
.authenticated()//都需要身份认证
|
||||
.and()
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.micro.fast.security.core.constant;
|
||||
|
||||
/**
|
||||
* 验证码校验常量类
|
||||
*/
|
||||
public class ValidateCodeConst {
|
||||
/**
|
||||
* 从session中获取验证码校验对象的key
|
||||
*/
|
||||
public static final String SESSION_KEY_IMAGE_CODE = "SESSION_KEY_IMAGE_CODE";
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.micro.fast.security.core.controller;
|
||||
|
||||
import com.micro.fast.security.core.constant.ValidateCodeConst;
|
||||
import com.micro.fast.util.ImageCodeUtil;
|
||||
import com.micro.fast.util.pojo.ImageCode;
|
||||
import org.springframework.social.connect.web.HttpSessionSessionStrategy;
|
||||
import org.springframework.social.connect.web.SessionStrategy;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 图片验证码控制层
|
||||
*/
|
||||
@RestController
|
||||
public class ValidateCodeController {
|
||||
|
||||
|
||||
private SessionStrategy sessionStrategy = new HttpSessionSessionStrategy();
|
||||
|
||||
@GetMapping("/code/image")
|
||||
public void createCode(HttpServletResponse response, HttpServletRequest request) throws IOException {
|
||||
//创建图片验证码
|
||||
ImageCode imageCode = ImageCodeUtil.createImageCode();
|
||||
//将验证码对象写入session中
|
||||
sessionStrategy.setAttribute(new ServletWebRequest(request), ValidateCodeConst.SESSION_KEY_IMAGE_CODE,imageCode);
|
||||
//将验证码写入到响应之中
|
||||
ImageIO.write(imageCode.getImage(),"JEPG",response.getOutputStream());
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.micro.fast.security.core.exception;
|
||||
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
|
||||
/**
|
||||
* 验证码校验异常
|
||||
*/
|
||||
public class ValidateCodeException extends AuthenticationException {
|
||||
|
||||
public ValidateCodeException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package com.micro.fast.security.core.filter;
|
||||
|
||||
import com.micro.fast.security.core.constant.ValidateCodeConst;
|
||||
import com.micro.fast.security.core.exception.ValidateCodeException;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
|
||||
import org.springframework.social.connect.web.HttpSessionSessionStrategy;
|
||||
import org.springframework.social.connect.web.SessionStrategy;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 图片验证码校验过滤器
|
||||
*/
|
||||
public class ValidateCodeFilter extends OncePerRequestFilter{
|
||||
|
||||
@Autowired
|
||||
private AuthenticationFailureHandler authenticationFailureHandler;
|
||||
/**
|
||||
* session操作工具类
|
||||
*/
|
||||
private SessionStrategy sessionStrategy = new HttpSessionSessionStrategy();
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(
|
||||
HttpServletRequest httpServletRequest
|
||||
, HttpServletResponse httpServletResponse
|
||||
, FilterChain filterChain)
|
||||
throws ServletException, IOException {
|
||||
//在登录post请求的时候对验证码进行校验
|
||||
if (StringUtils.equals("/user/login",httpServletRequest.getRequestURI())
|
||||
&& StringUtils.endsWithIgnoreCase(httpServletRequest.getMethod(),"post")){
|
||||
try{
|
||||
|
||||
//校验验证码是否正确
|
||||
validate(new ServletWebRequest(httpServletRequest));
|
||||
}catch(ValidateCodeException e){
|
||||
//进行身份认证失败后的处理
|
||||
authenticationFailureHandler.onAuthenticationFailure(httpServletRequest,httpServletResponse,e);
|
||||
}
|
||||
}else {
|
||||
//直接执行后续的过滤器
|
||||
filterChain.doFilter(httpServletRequest,httpServletResponse);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证码校验
|
||||
* @param servletWebRequest
|
||||
*/
|
||||
//todo 实现验证码校验的逻辑
|
||||
private Boolean validate(ServletWebRequest servletWebRequest) {
|
||||
sessionStrategy.getAttribute(servletWebRequest, ValidateCodeConst.SESSION_KEY_IMAGE_CODE);
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
-7
@@ -1,7 +0,0 @@
|
||||
package com.micro.fast.security.core.validate.code;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class ImageCode {
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.micro.fast.util;
|
||||
|
||||
import com.micro.fast.util.pojo.ImageCode;
|
||||
|
||||
/**
|
||||
* 验证码工具类
|
||||
*/
|
||||
public class ImageCodeUtil {
|
||||
//TODO 实现验证码生成类
|
||||
public static ImageCode createImageCode(){
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.micro.fast.util.pojo;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 图片验证码
|
||||
*/
|
||||
public class ImageCode {
|
||||
|
||||
/**
|
||||
* 图片
|
||||
*/
|
||||
private BufferedImage image;
|
||||
|
||||
/**
|
||||
* 验证码
|
||||
*/
|
||||
private String code;
|
||||
|
||||
/**
|
||||
* 过期时间
|
||||
*/
|
||||
private LocalDateTime expireTime;
|
||||
|
||||
public ImageCode() {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param image 验证码图片对象
|
||||
* @param code 验证码
|
||||
* @param in 在多长时间内过期
|
||||
*/
|
||||
public ImageCode(BufferedImage image, String code, int in) {
|
||||
this.image = image;
|
||||
this.code = code;
|
||||
this.expireTime = LocalDateTime.now().plusSeconds(in);
|
||||
}
|
||||
|
||||
public BufferedImage getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public void setImage(BufferedImage image) {
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public LocalDateTime getExpireTime() {
|
||||
return expireTime;
|
||||
}
|
||||
|
||||
public void setExpireTime(LocalDateTime expireTime) {
|
||||
this.expireTime = expireTime;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user