简体   繁体   English

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

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

Im working on a spring boot 2 micro services . 我正在从事spring boot 2微服务。 now im planning to secure to my rest calls using the OAUTH2 . 现在我打算使用OAUTH2确保我的休息电话安全。

I found lot of articles reg that Spring 2 + OAUTH2 integration but does not match with my requirement , all of them use tables and secure calls using the roles , 我发现有很多关于Spring 2 + OAUTH2集成的文章,但与我的要求不符,所有文章都使用表和使用角色的安全调用,

My application login works on Single Sign on using the SAML (SSO) , my requirement is to only authorize the each request . 我的应用程序登录使用SAML(SSO)进行单点登录,我的要求是仅授权每个请求。 what are the best way to do that . 最好的方法是什么?

  1. do i really need table to store the token for the user , since login is already happened using SSO ? 因为使用SSO已经发生登录,我是否真的需要表为用户存储令牌?
  2. only thing here is to authorize request irrespective of the roles of the user . 这里唯一的事情就是不管用户的角色如何都授权请求。

Any suggestions or github link to match the simple requirement will be appreciated . 任何建议或github链接,以符合简单的要求,将不胜感激。

@premKumarR @premKumarR

For your comments on which is better in-memory v/s JDBC. 对于您在内存v / s JDBC中哪个更好的评论。 For Spring Docs, 对于Spring Docs,

Here's a description with some discussion of each of them 这是对它们的讨论的描述

The default InMemoryTokenStore is perfectly fine for a single server (ie low traffic and no hot swap to a backup server in the case of failure). 默认的InMemoryTokenStore非常适合单个服务器(例如,低流量,并且在发生故障的情况下不与备份服务器进行热交换)。 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. JdbcTokenStore是同一事物的JDBC版本,它将令牌数据存储在关系数据库中。 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. 如果可以在服务器之间共享数据库,请使用JDBC版本;如果只有一个,则可以扩展同一服务器的实例;如果有多个组件,则可以使用授权和资源服务器。 To use the JdbcTokenStore you need "spring-jdbc" on the classpath. 要使用JdbcTokenStore,您需要在类路径上使用“ spring-jdbc”。

Docs Link: https://projects.spring.io/spring-security-oauth/docs/oauth2.html 文件连结: https : //projects.spring.io/spring-security-oauth/docs/oauth2.html

OAuth2 has different implementations of creating Tokens for authentication. OAuth2具有创建用于身份验证的令牌的不同实现。 By default it creates tokens via random value and handles everything except for the persistence of the tokens which it delegates to a TokenStore . 默认情况下,它通过随机值创建令牌并处理所有TokenStore除了将其委托给TokenStore的令牌的持久性TokenStore The default store is an in-memory implementation, but there are some other implementations available. 默认存储是内存中的实现,但是还有其他一些可用的实现。

The JdbcTokenStore is the JDBC version of the same thing, which stores token data in a relational database. JdbcTokenStore是同一事物的JDBC版本,它将令牌数据存储在关系数据库中。

The JSON Web Token (JWT) version of the store but does not persist data. 存储的JSON Web Token (JWT)版本,但不保留数据。

So to answer your questions 所以回答你的问题

  • do i really need table to store the token for the user , since login is already happened using SSO ? 因为使用SSO已经发生登录,我是否真的需要表为用户存储令牌?

    Not necessary. 不必要。 As i understand you only intend to authenticate not to generate the tokens. 据我了解,您仅打算进行身份验证而不生成令牌。

  • only thing here is to authorize request irrespective of the roles of the user . 这里唯一的事情就是不管用户的角色如何都授权请求。

    You can use WebSecurityConfigurerAdapter to vaildate incoming request. 您可以使用WebSecurityConfigurerAdapter使传入的请求失效。 eg as below 例如如下

    public class Configuration extends WebSecurityConfigurerAdapter { 公共类配置扩展了WebSecurityConfigurerAdapter {

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM