小编Vic*_*ong的帖子

使用@ExceptionHandler处理spring安全性身份验证异常

我正在使用Spring MVC @ControllerAdvice@ExceptionHandler处理REST Api的所有异常.它适用于Web mvc控制器抛出的异常,但它不适用于spring安全自定义过滤器抛出的异常,因为它们在调用控制器方法之前运行.

我有一个自定义弹簧安全过滤器,它执行基于令牌的身份验证:

public class AegisAuthenticationFilter extends GenericFilterBean {

...

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

        try {

            ...         
        } catch(AuthenticationException authenticationException) {

            SecurityContextHolder.clearContext();
            authenticationEntryPoint.commence(request, response, authenticationException);

        }

    }

}
Run Code Online (Sandbox Code Playgroud)

使用此自定义入口点:

@Component("restAuthenticationEntryPoint")
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint{

    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authenticationException) throws IOException, ServletException {
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authenticationException.getMessage());
    }

}
Run Code Online (Sandbox Code Playgroud)

并使用此类来处理全局异常:

@ControllerAdvice
public class RestEntityResponseExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler({ InvalidTokenException.class, AuthenticationException.class })
    @ResponseStatus(value = HttpStatus.UNAUTHORIZED) …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc spring-security

79
推荐指数
5
解决办法
8万
查看次数

Gson序列化接口/抽象类

如果我注册一个JsonSerializer带有接口抽象类,如:

GsonBuilder builder = new GsonBuilder()
    .registerTypeAdapter(MyInterface.class, new MySerializer());
Gson gson = builder.create();

MyInterface i = new MyConcreteClass();
String json = gson.toJson(i); // <--- Serializer is not invoked
Run Code Online (Sandbox Code Playgroud)

不会调用序列化程序gson.toJson().

但是,如果我有一个包含MyInterface以下内容的类:

public class MyAnotherClass {
    MyInterface i;
    // ...
}

MyAnotherClass c = new MyAnotherClass()
String json = gson.toJson(c); // <--- Serializer is invoked
Run Code Online (Sandbox Code Playgroud)

调用序列化程序.

对我来说,这是一种不一致的行为.它对成员变量的工作方式是查找声明的类型而不是实际的类型,但它对"根"对象的工作方式是不同的.

这是一个错误/缺陷还是预期的行为?如果这是预期的,背后的任何原因?

java serializer gson

3
推荐指数
1
解决办法
4918
查看次数

标签 统计

gson ×1

java ×1

serializer ×1

spring ×1

spring-mvc ×1

spring-security ×1