【问题标题】:Spring boot Microservices authorizationSpring Boot 微服务授权
【发布时间】:2020-09-27 08:51:31
【问题描述】:

我有两个 Spring Boot 应用程序。后端部分 - 可以访问数据库,它用作 Rest API 和管理面板。前端部分 - 使用 Rest API 为客户端显示信息。

所以我对客户端部分(前端)的配置安全性有疑问,对于管理面板也是如此,授权​​是通过会话实现的。以前,客户端部分的授权是通过 JWT 令牌实现的,但我不太了解如何为每个单独的客户端存储令牌并在向 Rest API 发送请求时使用它。

这是我的安全配置:

@Configuration
@EnableWebSecurity
@ComponentScan(basePackages = "kg.nurtelecom.cashbackapi")
public class SecurityConfig {
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Configuration
    @Order(1)
    public static class RestApiSecurityConfig extends WebSecurityConfigurerAdapter {
        @Autowired
        private JwtAuthenticationTokenFilter jwtAuthFilter;

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .csrf().disable()
                    .antMatcher("/api/**")
                    .authorizeRequests()
                    .antMatchers("/api/authenticate").permitAll()
                    .antMatchers("/api/**").permitAll()
                    .and()
                    .addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class);

            http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        }
    }

    @Configuration
    @Order(2)
    public static class AdminSecurityConfig extends WebSecurityConfigurerAdapter {
        @Autowired
        @Qualifier("customUserDetailsService")
        private UserDetailsService userDetailsService;

        @Autowired
        public void configureGlobalSecurity(AuthenticationManagerBuilder auth) {
            auth.authenticationProvider(authenticationProvider());
        }

        @Bean
        public PasswordEncoder getPasswordEncoder() {
            return new BCryptPasswordEncoder(8);
        }

        @Bean
        public DaoAuthenticationProvider authenticationProvider() {
            DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
            authenticationProvider.setUserDetailsService(userDetailsService);
            authenticationProvider.setPasswordEncoder(getPasswordEncoder());
            return authenticationProvider;
        }


        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .authorizeRequests()
                    .antMatchers("/**").authenticated()
                    .antMatchers("/login")
                    .permitAll()
                    .anyRequest()
                    .authenticated()
                    .and()
                    .formLogin()
                    .usernameParameter("username")
                    .passwordParameter("password")
                    .loginPage("/login")
                    .failureUrl("/login?error")
                    .permitAll()
                    .and()
                    .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                    .logoutSuccessUrl("/login");
        }

        @Override
        public void configure(WebSecurity web) throws Exception {
            web
                    .ignoring()
                    .antMatchers("/resources/**", "/static/**", "/assets/**", "/css/**", "/js/**");
        }
    }

}

那么是否可以使用 JWT 令牌在两个 Spring Boot 应用程序之间配置授权?

【问题讨论】:

  • 看看这是否有帮助 shorturl.at/nvxDW

标签: spring-boot spring-security authorization microservices jwt-auth


【解决方案1】:

您必须在需要时请求它,例如,当前一个已过期时,当您获得它时,您必须将其存储在客户端的某个位置。

例如,本地存储或 cookie,因此无论何时您需要调用后端,都可以将其附加到授权标头中的请求中

【讨论】:

  • 是的,我明白了。但是有什么方便的方法来实现这个吗?我的意思是在 Spring Boot 应用程序中不使用本地存储或 cookie。
猜你喜欢
  • 2019-05-25
  • 1970-01-01
  • 2019-03-15
  • 2019-04-05
  • 2018-08-29
  • 2020-03-11
  • 2022-01-08
  • 2019-02-20
  • 2020-12-09
相关资源
最近更新 更多