当前位置: 首页 > 知识库问答 >
问题:

Spring boot微服务授权

谭健柏
2023-03-14

我有两个Spring启动应用程序。后端部分-可以访问数据库,它用作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应用程序之间配置授权吗?

共有1个答案

呼延哲
2023-03-14

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

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

 类似资料:
  • 我有一个多租户项目,它将调用多个微服务来执行特定任务。 我希望微服务从发送的请求中了解要使用哪个DB,因为每个租户都将使用微服务,但是,租户将拥有自己的DB。我有另一个解决方案,它有一个处理API密钥管理的Web项目。 比方说,API密钥管理位于域:portal.example.com 当 tenant.example.com 在 microservice.example.com 调用微服务时,我

  • 我正试图从现有的应用程序中设计一个具有相当标准的用户管理的微服务:具有认证和授权,并存储用户数据。 我正在开发一个授权服务器来管理用户身份验证和授权,使用作为授权。在另一方面,我必须存储用户的信息/配置文件。 问题:授权服务器应该管理: 授权和用户API?因此,其他微服务可以联系上的授权服务器以获取当前用户,也可以联系以获取完整的用户列表。 还是只有授权,我必须创建用户微服务?因此授权服务器只公开

  • 我们正在尝试找出IPC身份验证和授权的最佳实践。我会解释的。我们有一个基于微服务的体系结构SaaS和一个专门的认证服务。该服务负责执行身份验证和管理身份验证令牌(JWT)。 现在的问题是如何验证和授权由其他服务发起的请求(没有特定用户的上下文)? 我们是否应该为每个服务生成一个专用用户,并像对待系统中的任何其他用户一样对待它(具有适当的权限)? 我们是否应该在服务之间部署“硬编码”/动态令牌? 还

  • 我最近读了不少关于微服务的文章,尤其是关于AuthN和Authz的文章。在很大程度上,这一切都很有意义,我可以看到这一切应该如何工作。 (注意--现在我只实现了客户端凭据授予和资源所有者密码凭据授予,因为我只是想看看它是如何工作的,而且用curl调用它们更容易。但我不知道这有什么不同)

  • 17.1 编写服务启动入口 package com.clsaa.edu.springboot; import com.clsaa.edu.springboot.bean.Product; import com.clsaa.edu.springboot.mapper.ProductMapper; import org.springframework.boot.SpringApplicati

  • 我有多个微服务。客户端可以通过API网关调用这些微服务,微服务之间也可以相互通信。 理想情况下,请求将来自拥有所有权限的用户的API网关。例如,如果用户(浏览器)需要来自微服务A的数据,则只将该角色授予用户,如果内部微服务A需要来自B的数据(rest call),则不应将该角色分配给用户。 要求:如何限制/授权微服务之间的内部通信,以便只有经授权的微服务才能呼叫其他服务。 选项: > 将所有角色分