gpt4 book ai didi

java - 如何在 spring boot 中从未经授权的响应中删除变量

转载 作者:行者123 更新时间:2023-12-03 18:52:22 32 4
gpt4 key购买 nike

在检查用户未授权时,我有这样的回复。

是否有可能从未经授权的响应中删除路径?因为它没有为用户提供有值(value)的信息

{
"timestamp": "2021-03-18T09:16:09.699+0000",
"status": 401,
"error": "Unauthorized",
"message": "Unauthorized",
"path": "/test/v1/api/test.com/config/settings"

我的配置是这样的

public class ResourceConfig extends ResourceServerConfigurerAdapter {


@Override
public void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.csrf().disable()
.cors();

httpSecurity
.anonymous().disable()
.requestMatchers().antMatchers("/api/**")
.and()
.authorizeRequests()
.antMatchers("/api/**")
.authenticated()
.and()
.exceptionHandling()
.accessDeniedHandler(new OAuth2AccessDeniedHandler());

}

最佳答案

添加@linhx 使用自定义AuthenricationEntryPoint 的想法,您可以使用HandlerExceptionResolver 解析为页面

您可以获得不同方法的详细比较here .

@Component
public class ABAuthenticationEntryPoint implements AuthenticationEntryPoint {

protected final Logger logger = LoggerFactory.getLogger(ABAuthenticationEntryPoint.class);

private final String realmName = "CustomRealm";

@Autowired
@Qualifier("handlerExceptionResolver")
private HandlerExceptionResolver resolver;

@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
resolver.resolveException(request, response, null, authException);
}
}

HandlerExceptionResolver 使用处理程序 (HandlerMethod) 获取 Controller 类并扫描它以查找使用 @ExceptionHandler 注释的方法。如果此方法之一与异常 (ex) 匹配,则调用此方法以处理异常。 (否则返回 null 表示此异常解析器不负责任)。

因此,使用@ControllerAdvice 添加一个类:

@ExceptionHandler(value = InsufficientAuthenticationException.class)
public ResponseEntity<Object> handleInsufficientAuthenticationException(InsufficientAuthenticationException ex) {
String methodName = "handleInsufficientAuthenticationException()";
return buildResponseEntity(HttpStatus.UNAUTHORIZED, null, null, ex.getMessage(), null);
}

private ResponseEntity<Object> buildResponseEntity(HttpStatus status, HttpHeaders headers, Integer internalCode, String message, List<Object> errors) {
ResponseBase response = new ResponseBase()
.success(false)
.message(message)
.resultCode(internalCode != null ? internalCode : status.value())
.errors(errors != null
? errors.stream().filter(Objects::nonNull).map(Objects::toString).collect(Collectors.toList())
: null);

return new ResponseEntity<>((Object) response, headers, status);
}

SecurityConfig 类:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
protected final Logger logger = LoggerFactory.getLogger(SecurityConfig.class);

@Autowired
private ABAuthenticationEntryPoint authenticationEntryPoint;

@Override
protected void configure(HttpSecurity http) throws Exception {
http.
.....
.and()
.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint); //AuthenticationEntryPoint has to be the last
}
}

最后,根据您buildResponseEntity

的方式,您将得到如下内容
{
"success": false,
"resultCode": 401,
"message": "Full authentication is required to access this resource"
}

关于java - 如何在 spring boot 中从未经授权的响应中删除变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66688115/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com