SpringSecurity前后端分离配置(二)

前言

文档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));
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值