Skip to content

无权限 异常响应处理

问题

访问任意 OAuth 2 资源服务时 Token 异常

异常HTTP 响应状态码响应数据
无效 Token401
无 Token401

配置

shell
package cloud.xuxiaowei.oauth2.point;

import lombok.extern.slf4j.Slf4j;
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;

/**
 * 资源服务 身份验证入口点
 *
 * @author xuxiaowei
 * @since 0.0.1
 */
@Slf4j
public class ResourceServerAuthenticationEntryPoint implements AuthenticationEntryPoint {

	@Override
	public void commence(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException authException) throws IOException, ServletException {

		// 打印异常日志
		log.error("OAuth 2.1 无权限 异常处理:", authException);

		// 使用 HttpServletResponse 自定义响应

	}

}
shell
package cloud.xuxiaowei.oauth2.point;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import java.io.IOException;

/**
 * 资源服务 身份验证入口点
 *
 * @author xuxiaowei
 * @since 0.0.1
 */
@Slf4j
public class ResourceServerAuthenticationEntryPoint implements AuthenticationEntryPoint {

	@Override
	public void commence(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException authException) throws IOException, ServletException {

		// 打印异常日志
		log.error("OAuth 2.1 无权限 异常处理:", authException);

		// 使用 HttpServletResponse 自定义响应

	}

}

使用

shell
package cloud.xuxiaowei.oauth2.config;

import cloud.xuxiaowei.oauth2.point.ResourceServerAuthenticationEntryPoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

/**
 * @author xuxiaowei
 * @since 0.0.1
 */
@Configuration
public class ResourceServerConfig {

	@Bean
	public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {

		http.oauth2ResourceServer().authenticationEntryPoint(new ResourceServerAuthenticationEntryPoint());

		return http.build();
	}

}
shell
package cloud.xuxiaowei.oauth2.config;

import cloud.xuxiaowei.oauth2.point.ResourceServerAuthenticationEntryPoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

/**
 * @author xuxiaowei
 * @since 0.0.1
 */
@Configuration
public class ResourceServerConfig {

	@Bean
	public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {

		http.oauth2ResourceServer(oauth2ResourceServerCustomizer -> {
			oauth2ResourceServerCustomizer.authenticationEntryPoint(new ResourceServerAuthenticationEntryPoint());
		});

		return http.build();
	}

}