Spring boot 2 + Oauth2 - 保护微服务中的其余调用

Spring boot 2 + Oauth2 - Securing the Rest Calls in Microservices

我正在开发 spring boot 2 微服务。现在我计划使用 OAUTH2 来保护我的休息电话。

我发现很多文章都提到 Spring 2 + OAUTH2 集成但不符合我的要求,它们都使用 tables 并使用角色安全调用,

我的应用程序登录使用 SAML (SSO) 在单点登录上工作,我的要求是只授权每个请求。最好的方法是什么。

  1. 我真的需要 table 来为用户存储令牌吗,因为已经使用 SSO 登录了?
  2. 这里唯一的事情就是授权请求,而不考虑用户的角色。

任何符合简单要求的建议或 github link 将不胜感激。

@premKumarR

关于哪个内存更好的评论 v/s JDBC。 对于 Spring 个文档,

Here's a description with some discussion of each of them

The default InMemoryTokenStore is perfectly fine for a single server (i.e. low traffic and no hot swap to a backup server in the case of failure). Most projects can start here, and maybe operate this way in development mode, to make it easy to start a server with no dependencies.

The JdbcTokenStore is the JDBC version of the same thing, which stores token data in a relational database. Use the JDBC version if you can share a database between servers, either scaled up instances of the same server if there is only one, or the Authorization and Resources Servers if there are multiple components. To use the JdbcTokenStore you need "spring-jdbc" on the classpath.

文档 Link:https://projects.spring.io/spring-security-oauth/docs/oauth2.html

OAuth2 有不同的创建令牌以进行身份​​验证的实现。默认情况下,它通过随机值创建令牌并处理除委托给 TokenStore 的令牌的持久性之外的所有内容。默认存储是内存中实现,但还有一些其他实现可用。

JdbcTokenStore 是同一事物的 JDBC 版本,它将令牌数据存储在关系数据库中。

存储的 JSON Web Token (JWT) 版本但不持久化数据。

所以回答你的问题

  • 我真的需要 table 为用户存储令牌吗,因为已经使用 SSO 登录了?

    没有必要。据我了解,您只打算进行身份验证而不是生成令牌。

  • 这里唯一的事情就是不管用户的角色如何授权请求。

    您可以使用 WebSecurityConfigurerAdapter 验证传入的请求。例如如下

    public class 配置扩展 WebSecurityConfigurerAdapter {

    @Override
        public void configure(HttpSecurity http) throws Exception { 
            http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/authorization-server-1/**",
                  "/login").permitAll()
                .anyRequest().authenticated();
        }
    }