小编Nic*_*ola的帖子

使用@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万
查看次数

在RESTful API中存储身份验证令牌的位置

我已经开始设计RESTful API,我正在考虑如何处理身份验证.我想使用某种身份验证令牌,但我不能使用OAuth o类似的基础架构,所以我必须自己处理它.

此API的一个要求是它必须具有良好的性能,足以在需要扩展之前处理大量请求; 我担心的是如何在每个请求上尽可能少地验证令牌(完整性,到期,IP地址等)所需的时间.

我认为令牌应该是某种哈希,而不是包含用户信息的加密字符串,因为解密时间会很重.

我已经读过,我可以将令牌存储在内存中的散列表中,其中键是令牌,值是处理请求所需的用户信息,但是我如何在集群环境中将其设置为有效每个"节点"上的哈希表?

我是否应该在数据库表上放置令牌每次都按下数据库还手动处理过期票据的保留?

可能它对于这个问题并不重要,但我正在使用Spring MVC作为RESTfull API.

提前致谢.

authentication api rest token

10
推荐指数
2
解决办法
8400
查看次数

标签 统计

api ×1

authentication ×1

rest ×1

spring ×1

spring-mvc ×1

spring-security ×1

token ×1