【Spring Security Oauth2】构建资源服务器(三):自定义身份认证不通过和权限验证不通过响应返回数据

一、环境准备

【Spring Security Oauth2】构建资源服务器(一):构建服务

二、默认响应数据和自定义响应数据对比

1、默认响应数据

  • 请求头中未传入token
    在这里插入图片描述
  • 请求头中传入错误token
    在这里插入图片描述
  • 请求头中传入正确token,但是该token没有该接口的权限在这里插入图片描述

2、自定义响应数据

  • 请求头中未传入token
    在这里插入图片描述

  • 请求头中传入错误token
    在这里插入图片描述

  • 请求头中传入正确token,但是该token没有该接口的权限
    在这里插入图片描述

三、代码修改

1、新增UserAccessDeniedHandler类和UserAuthenticationEntryPoint类,分别实现AccessDeniedHandler接口和AuthenticationEntryPoint接口。

package com.cyun.sys.config.access;

import com.cyun.core.result.HttpStatus;
import com.cyun.core.result.ResultVO;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

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

/**
 * 用来解决认证过的用户访问无权限资源时的异常
 *
 * @author panfu.he
 */
@Component
public class UserAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException {
        httpServletResponse.setContentType("text/json;charset=utf-8");
        httpServletResponse.getWriter().print(ResultVO.error(HttpStatus.SC_UNAUTHORIZED, "访问权限认证未通过!!!"));
    }
}

package com.cyun.sys.config.access;

import com.cyun.core.result.HttpStatus;
import com.cyun.core.result.ResultVO;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

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

/**
 * 用来解决匿名用户访问token验证失败时的异常
 *
 * @author panfu.he
 */
@Component
public class UserAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException {
        httpServletResponse.setContentType("text/json;charset=utf-8");
        httpServletResponse.getWriter().print(ResultVO.error(HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED, "身份认证失败!!!"));
    }
}

2、修改ResourceServerConfig类的configure(ResourceServerSecurityConfigurer resources)资源管理方法。

在这里插入图片描述

	// 依赖注入
    private final UserAuthenticationEntryPoint userAuthenticationEntryPoint;
    private final UserAccessDeniedHandler userAccessDeniedHandler;

    /**
     * 资源管理
     *
     * @param resources 资源管理
     */
    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        //资源 id
        resources.resourceId(RESOURCE_ID)
                //验证令牌的服务
                .tokenServices(tokenService())
                .authenticationEntryPoint(userAuthenticationEntryPoint)
                .accessDeniedHandler(userAccessDeniedHandler);
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值