Spring OAuth2 资源服务器外部授权服务器



如何设置一个单独的Spring OAuth2资源服务器,它使用和第三方授权服务器

我看到的所有示例总是在同一应用程序中实现资源服务器和授权服务器。

我不想实现授权服务器,因为其他人会提供这个。

尝试过没有运气

@Configuration
@EnableResourceServer
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter

和 application.yml 包括

security:
oauth2:
resource:
userInfoUri: https://...../userinfo

在我的问题中添加一些进一步的细节:

据我了解 - 使用 OAuth 有 4 名玩家:

  • 资源所有者:一个人
  • 资源服务器:公开受保护 API 的服务器(受身份验证服务器保护)
  • 身份验证服务器:处理向客户端颁发访问令牌的服务器
  • 客户端:在资源所有者同意后访问资源服务器 API 的应用程序(例如网站)

我尝试了各种教程,但似乎都实现了自己的授权服务器

http://www.swisspush.org/security/2016/10/17/oauth2-in-depth-introduction-for-enterprises https://gigsterous.github.io/engineering/2017/03/01/spring-boot-4.html

或者是实现客户端播放器的示例

  • http://www.baeldung.com/spring-security-openid-connect
  • https://spring.io/guides/tutorials/spring-boot-oauth2/

我的问题是: 如何仅通过第三方身份验证服务器实现保护我的 REST API 的资源服务器,仅此而已。

我已经解决了这个问题 - 你所需要的只是:

@SpringBootApplication
@EnableResourceServer
public class ResourceServer {
public static void main(String[] args) {
SpringApplication.run(ResourceServer.class, args);
}
}

使用应用程序.yml,如在原始问题中发布的那样:

security:
oauth2:
resource:
userInfoUri: https://........userinfo

我创建了两个示例单独的应用程序,其中一个充当 oauth 客户端,另一个充当资源服务器,它们都使用外部身份验证服务器(在本例中为 facebook)。

示例中的场景如下,用户打开 app1(oauth 客户端)并被重定向到第一页,一旦他单击登录,他将被重定向到 facebook 登录,登录成功后,他将返回到第一页。如果他单击第一个按钮,将调用同一应用程序中的 api,并将出现在消息 1 标签旁边,如果他单击第二个按钮,将调用 app2(资源服务器)中的 api,并且消息将显示在消息 2 标签旁边。

如果检查日志,您会发现从 app1 到 app2 的 api 调用在请求参数中包含访问令牌。 app1 调用 app2 的日志

请在此处找到 git 存储库的源代码

这是 app1(oauth 客户端)的配置

App1 网络安全配置

@Configuration
@EnableOAuth2Sso
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**").authorizeRequests().antMatchers("/", "/login**", "/webjars/**", "/error**").permitAll()
.anyRequest().authenticated().and().logout().logoutSuccessUrl("/").permitAll().and().csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("OPTIONS");
config.addAllowedMethod("GET");
config.addAllowedMethod("POST");
config.addAllowedMethod("PUT");
config.addAllowedMethod("DELETE");
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}

App1 应用程序属性

security:
oauth2:
client:
clientId: <your client id>
clientSecret: <your client secret>
accessTokenUri: https://graph.facebook.com/oauth/access_token
userAuthorizationUri: https://www.facebook.com/dialog/oauth?redirect_url=https://localhost:8443/
tokenName: access_token
authenticationScheme: query
clientAuthenticationScheme: form
registered-redirect-uri: https://localhost:8443/
pre-established-redirect-uri: https://localhost:8443/
resource:
userInfoUri: https://graph.facebook.com/me
logging:
level:
org.springframework.security: DEBUG

这是 app2(资源服务器)的配置

App2 资源服务器配置

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
String[] ignoredPaths = new String[] { "/error", "/login", "/doLogut", "/home", "/pageNotFound", "/css/**",
"/js/**", "/fonts/**", "/img/**" };
@Value("${security.oauth2.resource.user-info-uri}")
private String userInfoUri;
@Value("${security.oauth2.client.client-id}")
private String clientId;
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers(ignoredPaths).permitAll().anyRequest().authenticated();
}
@Primary
@Bean
public UserInfoTokenServices tokenService() {
final UserInfoTokenServices tokenService = new UserInfoTokenServices(userInfoUri, clientId);
return tokenService;
}
}

App2 应用程序属性

security:
oauth2:
resource:
userInfoUri: https://graph.facebook.com/me
client:
client-id: <your client id>
logging:
level:
org.springframework.security: DEBUG  

这是 app1 控制器在 app2 上调用 api 的地方(hi2 api)

@RestController
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class UserController {
@Autowired
OAuth2RestTemplate restTemplate;
@RequestMapping("/user")
public Principal user(Principal principal) {
return principal;
}
@RequestMapping("/hi")
public String hi(Principal principal) {
return "Hi, " + principal.getName();
}
@RequestMapping("/hi2")
public String hi2(Principal principal) {
final String greeting = restTemplate.getForObject("http://127.0.0.1:8082/api/hello", String.class);
System.out.println(greeting);
return greeting;
}
}

最新更新