spring - 在 Spring Boot 中使用自定义 token 进行身份验证

标签 spring spring-boot spring-security spring-rest

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

  • 一个 spring boot 应用程序,它暴露了一些 rest api。
  • 与公开的 api 通信的前端。
  • 前端发送用于身份验证的自定义身份验证 token 。
  • 存储自定义身份验证 token 的数据库。

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

    此身份验证应该可用于我的 Spring Boot 应用程序中的所有 Controller 。有没有办法在默认情况下为每个休息请求执行此操作,而无需在每个 Controller 中明确放置身份验证?

    我了解 spring boot web 安全功能,但没有足够的信息来说明如何将这些与自定义 token 一起使用。

    最佳答案

    毫无疑问,spring-security 是要走的路。使用 Spring Boot,使用这个启动器:

      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
       </dependency>
    

    然后你必须在一些配置类中定义你的安全配置,例如:
    @EnableWebSecurity
    @Configuration
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
       private final BaseTokenService tokenService;
    
        @Bean
        public TokenAuthenticationService tokenAuthenticationService() {
            return new TokenAuthenticationServiceImpl(tokenService);
        }
    
        @Bean
        public TokenAuthenticationProvider tokenAuthenticationProvider() {
            return new TokenAuthenticationProvider(tokenAuthenticationService());
        }
    
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.authenticationProvider(tokenAuthenticationProvider());
        }
    
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
    
            TokenAuthenticationFilter tokenAuthenticationFilter = new TokenAuthenticationFilter(super.authenticationManager(), false);
    
            //session management
            http
                    .anonymous().disable()
                    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                    .and()
                    .csrf().disable();
    
            //filter
            http
                    .antMatcher("/api/secured/**")
                    .addFilterBefore(tokenAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
                    .authorizeRequests()
                    .anyRequest()
                    .authenticated();
        }
    
    }
    

    正如您在上面的配置中看到的,我使用了自定义身份验证过滤器 (tokenAuthenticationFilter)。它可能是一种可用于处理第三条语句的安全过滤器:前端发送用于身份验证的自定义身份验证 token 。
    它与 AuthenticationProvider 一起提供,Spring 安全组件根据安全过滤器提取的 token 验证用户身份验证。您必须根据需要提供所有 Token* 类的正确实现。

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

    spring 安全文档应该是要走的路:

    https://docs.spring.io/spring-security/site/docs/4.2.5.RELEASE/reference/htmlsingle/

    如果你想要一个示例教程:

    https://www.sylvainlemoine.com/2016/06/06/spring-saml2.0-websso-and-jwt-for-mobile-api/

    跳过 saml 部分,它在这里无关紧要,但请看一下 JWT(Json Web token )部分,它应该回答您的用例。

    关于spring - 在 Spring Boot 中使用自定义 token 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49731231/

    相关文章:

    java - 您对 Spring MVC 应用程序中的服务层使用什么命名约定?

    java - 无法使用 Java 8 运行 Spring 4 和 Tomcat 8

    java - 如何使用 Spring Security 更改登录的弹出行为?

    java - 没有这样的字段错误: NULL Spring Error

    java - 如何将属性文件中的数据加载到bean属性值中?

    java - 针对未经身份验证的用户的 Spring Security 404 页面

    java - 保存一对一关系: Null pointer Exception

    java - 在没有@Repository 实现的情况下使用@NoRepositoryBean 运行 Spring 集成测试

    grails - 使用acegi以编程方式在Grails中分配角色

    spring - CQ5 Spring 集成