Oauth2的异常处理,统一封装

一.token的认证自定义异常

1.在资源服务器配置
2.创建CustomOAuthEntryPoint

package com.othp.core.config;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

@Component
public class CustomOAuthEntryPoint implements AuthenticationEntryPoint {

	@Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
                         AuthenticationException authException) throws ServletException {
		Map map = new HashMap();
        map.put("code", "40001");
        map.put("message", "token无效!");
//        map.put("message", authException.getMessage());
        map.put("data", request.getServletPath());
        response.setContentType("application/json");
        //请求正常码 200
        response.setStatus(HttpServletResponse.SC_OK);
//      response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.writeValue(response.getOutputStream(), map);
        } catch (Exception e) {
            throw new ServletException();
        }
    }

}

3.在资源服务器的配置authenticationEntryPoint

package com.othp.mine.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;

import com.othp.core.config.CustomOAuthEntryPoint;

@Configuration
@EnableResourceServer //@这个注解就决定了这是个资源服务器。
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
	
	@Autowired
	private CustomOAuthEntryPoint customOAuthEntryPoint;

    @Override
    public void configure(HttpSecurity http) throws Exception {

    	//post请求默认的都开启了csrf的模式,所有post请求都必须带有token之类的验证信息才可以进入登陆页面,这边是禁用csrf模式
        http.csrf().disable();

        http.authorizeRequests()  //方法有多个子节点,每个匹配器按其声明的顺序进行考虑。
            .antMatchers(
            		//waggerui 忽略
            		"/webjars/**",
                    "/resources/**",
                    "/swagger-ui.html",
                    "/swagger-resources/**",
                    "/v2/api-docs",
                    //系统使用
            		"/CK030201/*",
            		"/Common/getPhoneCode",
            		"/SJ030101/*",
            		"/Common/getToken")
//            .antMatchers("**")
            .permitAll(); //不需要认证
        http.authorizeRequests()  
        .anyRequest()
        .authenticated(); //用户进行身份验证
    }
    
    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
    	//bearer token的验证,异常自定义返回
        resources.authenticationEntryPoint(customOAuthEntryPoint);
    }

}

3.调用相应接口。传错误的bearer
在这里插入图片描述

二、token获取时,账号密码错误异常自定义

待更新。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值