处理器模板

1:配置登录成功方发处理器 ,实现 AuthenticationSuccessHandler 接口

package cn.woniu.handler;

import cn.woniu.util.JWTUtil;
import cn.woniu.util.ResponseResult;
import com.alibaba.fastjson.JSON;

import org.apache.catalina.filters.ExpiresFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import sun.text.normalizer.ICUBinary;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 前后端分离的项目情况下,登录成功后,返回的不再是一个页面地址,,还是一个json
 * 处理用户登录成功后返回数据:比如用户等信息
 */
@Component
public class LoginSuccessHandler implements AuthenticationSuccessHandler {



    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    @Override
    public void onAuthenticationSuccess(HttpServletRequest request,
                                        HttpServletResponse response,
                                        Authentication authentication)
            throws IOException, ServletException {

        /**
         * 获取登录成功的用户信息
         */
        User user = (User) authentication.getPrincipal();
        try {
            redisTemplate.opsForValue().set("jwt:"+user.getUsername(),
                    JWTUtil.createJWT(user.getUsername()));
        } catch (Exception e) {
            e.printStackTrace();
        }

        //设置字符串
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        String json = JSON.toJSONString(ResponseResult.SECCUSS);
        writer.print(json);
        writer.flush();
        writer.close();
    }
}

2:配置登录失败方法处理器:实现 AuthenticationFailureHandler 接口 

package cn.woniu.handler;

import cn.woniu.util.ResponseResult;
import com.alibaba.fastjson.JSON;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 登录失败后进入,返回给前端提示信息
 */
public class LoginFildHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        //设置字符串
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        String json = JSON.toJSONString(ResponseResult.FAIL);
        writer.print(json);
        writer.flush();
        writer.close();
    }
}

3:配置用户未登录直接访问我们系统资源,实现 AuthenticationEntryPoint 接口

package cn.woniu.handler;

import cn.woniu.util.ResponseResult;
import com.alibaba.fastjson.JSON;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 前后端分离项目情况下,用户未登录直接访问系统资源会被拦截
 */
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {


    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
//设置字符串
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        String json = JSON.toJSONString(ResponseResult.NOLOGIN);
        writer.print(json);
        writer.flush();
        writer.close();
    }
}

4:配置用户没有该资源访问权限情况下,实现 AccessDeniedHandler 接口

package cn.woniu.handler;

import cn.woniu.util.ResponseResult;
import com.alibaba.fastjson.JSON;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 虽然你知道用户名和密码
 * 但是,拦截你没有权限访问该资源操作
 *
 */
public class MyAccessDeniedHandler  implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        //设置字符串
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        String json = JSON.toJSONString(ResponseResult.NOAUTH);
        writer.print(json);
        writer.flush();
        writer.close();
    }
}

5:用户退出系统,实现LogoutSuccessHandler 接口

package cn.woniu.handler;

import cn.woniu.util.ResponseResult;
import com.alibaba.fastjson.JSON;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


public class MyLogoutSuccessHandler implements LogoutSuccessHandler{

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //设置字符串
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        String json = JSON.toJSONString(ResponseResult.LOGOUT);
        writer.print(json);
        writer.flush();
        writer.close();
    }
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值