前言
文档1:https://www.cnblogs.com/guos/archive/2019/10/02/11617243.html
文档2:
配置
自己的配置,结合了其他文档
import com.website.server.system.security.hander.LoginFailureHandler;
import com.website.server.system.security.hander.LoginSuccessHandler;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
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;
/**
* @author :qilong sun
* @date :Created in 2019/11/27 16:56
* @description:security配置
* @modified By:
* @version: V1.0$
*/
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
// 开启授权认证
httpSecurity.authorizeRequests().anyRequest().authenticated();
// 配置登录
httpSecurity.formLogin().usernameParameter("loginAccount").passwordParameter("loginPwd").loginProcessingUrl("/toLogin");
// 登录成功处理
httpSecurity.formLogin().successHandler(new LoginSuccessHandler());
// 登录失败处理
httpSecurity.formLogin().failureHandler(new LoginFailureHandler());
// csrf配置
httpSecurity.csrf();
// 开启跨域共享,跨域伪造请求限制=无效
httpSecurity.cors().and().csrf().disable();
}
}
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @author :qilong sun
* @date :Created in 2019/12/11 13:48
* @description:登录成功处理
* @modified By:
* @version: V1.0$
*/
public class LoginSuccessHandler implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
httpServletResponse.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
Map<String, String> map = new HashMap<>();
map.put("code","200");
map.put("msg","登录成功");
httpServletResponse.getWriter().write(JSONObject.toJSONString(map));
}
}
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @author :qilong sun
* @date :Created in 2019/12/11 14:49
* @description:登录失败处理
* @modified By:
* @version: V1.0$
*/
public class LoginFailureHandler implements AuthenticationFailureHandler {
@Override
public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException {
httpServletResponse.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
Map<String, String> map = new HashMap<>();
if(e instanceof AccessDeniedHandler){
map.put("code","401");
map.put("msg","权限不足");
}else if(e instanceof AuthenticationEntryPoint){
map.put("code","401");
map.put("msg","登录过期或未登录");
}else if(e instanceof AccountExpiredException){
map.put("code","401");
map.put("msg","账户过期");
}else if(e instanceof BadCredentialsException){
map.put("code","401");
map.put("msg","坏的凭证");
}else if(e instanceof DisabledException){
map.put("code","401");
map.put("msg","账户不可用");
}else if(e instanceof CredentialsExpiredException){
map.put("code","403");
map.put("msg","证书过期");
}else{
map.put("code","500");
map.put("msg","登录失败");
}
httpServletResponse.getWriter().write(JSONObject.toJSONString(map));
}
}