package com.taoke.autopay.config;
|
|
import net.sf.json.JSONObject;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.http.HttpStatus;
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
import org.springframework.security.core.AuthenticationException;
|
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
|
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;
|
|
@EnableWebSecurity
|
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
private final static String[] STATIC_RESOURCE_PATHS = new String[]{
|
"/**/*.html", "/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg", "/**/*.jpeg", "/**/*.gif", "/**/*.xml", "/**/font/*", "/**/fonts/*"
|
};
|
|
private Logger logger = LoggerFactory.getLogger(WebSecurityConfig.class);
|
|
|
/**
|
* @author hxh
|
* @description 请求之前的验证器
|
* @date 16:51 2022/5/19
|
* @return
|
**/
|
class PreRequestVerifyFilter extends OncePerRequestFilter {
|
|
//处理验证码出错
|
private AuthenticationFailureHandler verifyCodeFailureHandler = new AuthenticationFailureHandler() {
|
@Override
|
public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
|
httpServletResponse.setContentType("application/json;charset=UTF-8");
|
JSONObject jsonObject = new JSONObject();
|
jsonObject.put("code", 11);
|
jsonObject.put("msg", "验证码错误");
|
httpServletResponse.getWriter().print(jsonObject);
|
}
|
};
|
|
//处理没有权限
|
private AuthenticationFailureHandler authenticationFailureHandler = new AuthenticationFailureHandler() {
|
@Override
|
public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
|
httpServletResponse.setStatus(HttpStatus.FORBIDDEN.value());
|
}
|
};
|
|
@Override
|
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
|
|
}
|
}
|
|
|
@Override
|
protected void configure(HttpSecurity http) throws Exception {
|
http.headers().frameOptions().disable();
|
http.authorizeRequests()
|
.antMatchers("*/**").permitAll().and().csrf().disable();
|
// http.addFilterBefore(new PreRequestVerifyFilter(), UsernamePasswordAuthenticationFilter.class);
|
}
|
|
|
|
|
}
|