相关疑难解决方法(0)

基于Spring Security Token的身份验证

我有一个休息api,我在使用spring security Basic Authorization进行身份验证,其中客户端为每个请求发送用户名和密码.现在,我想实现基于令牌的身份验证,我将在用户首先进行身份验证时在响应头中发送令牌.对于进一步的请求,客户端可以在标头中包含该标记,该标记将用于向用户验证资源.我有两个身份验证提供程序tokenAuthenticationProvider和daoAuthenticationProvider

@Component
public class TokenAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    private TokenAuthentcationService service;

    @Override
    public Authentication authenticate(final Authentication authentication) throws AuthenticationException {

        final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        final HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        final String token = request.getHeader(Constants.AUTH_HEADER_NAME);
        final Token tokenObj = this.service.getToken(token);
        final AuthenticationToken authToken = new AuthenticationToken(tokenObj);
        return authToken;
    }

     @Override
        public boolean supports(final Class<?> authentication) {
            return AuthenticationToken.class.isAssignableFrom(authentication);
        }
}
Run Code Online (Sandbox Code Playgroud)

在daoAuthenticationProvider中,我设置自定义userDetailsS​​ervice并通过从数据库中获取用户登录详细信息进行身份验证(只要使用授权传递用户名和密码,该工作正常:基本bGllQXBpVXNlcjogN21wXidMQjRdTURtR04pag ==作为标头)

但是当我使用X-AUTH-TOKEN(即Constants.AUTH_HEADER_NAME)在标头中包含token时,不会调用tokenAuthenticationProvider.我收到错误了

{"timestamp":1487626368308,"status":401,"error":"Unauthorized","message":"Full authentication is required to access this resource","path":"/find"}
Run Code Online (Sandbox Code Playgroud)

以下是我添加身份验证提供程序的方法.

    @Override
    public void …
Run Code Online (Sandbox Code Playgroud)

java spring-security

26
推荐指数
1
解决办法
5万
查看次数

Spring Boot中使用自定义令牌进行身份验证

我需要保护我的 Spring Boot 应用程序,这就是我所拥有的:

  1. 一个 Spring Boot 应用程序,公开了一些 REST API。
  2. 与公开的 api 通信的前端。
  3. 前端发送用于身份验证的自定义身份验证令牌。
  4. 存储自定义身份验证令牌的数据库。

因此,本质上我的前端将向我的 Spring Boot 应用程序发送一个休息请求以及身份验证令牌,并且我的 Spring Boot 应用程序将查询数据库以查看身份验证令牌是否有效。

此身份验证应该适用于我的 Spring Boot 应用程序中的所有控制器。有没有一种方法可以默认为每个休息请求执行此操作,而无需在每个控制器中显式放置身份验证?

我了解 Spring Boot Web 安全功能,但没有足够的信息来说明如何将这些功能与自定义令牌一起使用。

spring spring-security spring-boot spring-rest

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

spring-security ×2

java ×1

spring ×1

spring-boot ×1

spring-rest ×1