如何设置单独的Spring OAuth2 ResourceServer,它使用和第三方AuthorizationServer
我看到的所有示例都始终在同一个应用程序中实现ResourceServer和AuthorizationServer。
我不想实施AuthorizationServer,因为其他人会提供此功能。
尝试过没有运气
@Configuration
@EnableResourceServer
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter
application.yml包括
security:
oauth2:
resource:
userInfoUri: https://...../userinfo
根据我的理解 - 与OAuth有4名球员:
我尝试了各种教程,但似乎都实现了自己的授权服务器
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
或是实施客户端播放器的示例
我的问题是: 如何通过第三方认证服务器实现仅保护我的REST API的资源服务器,仅此而已。
答案 0 :(得分:2)
我已经解决了这个问题 - 你需要的只是:
@SpringBootApplication
@EnableResourceServer
public class ResourceServer {
public static void main(String[] args) {
SpringApplication.run(ResourceServer.class, args);
}
}
将application.yml发布在原始问题中:
security:
oauth2:
resource:
userInfoUri: https://........userinfo
答案 1 :(得分:0)
我已经创建了两个样本独立的应用程序,其中一个充当oauth客户端,另一个充当资源服务器,并且两个都使用外部身份验证服务器(在此示例中为facebook)。
该示例中的场景如下,用户打开app1(oauth客户端)并重定向到首页,单击登录后,将重定向到facebook登录,成功登录后,他将返回第一页。如果他单击第一个按钮,则将在同一应用程序中对api进行调用,并在消息1标签旁边显示,如果他单击第二个按钮,则将对app2(资源服务器)中的api进行调用完成后,该消息将显示在消息2标签旁边。
如果您检查了日志,您会发现从app1到app2的api调用在请求参数中包含访问令牌。 Logs for app1 calling app2
请在git存储库here
中找到源代码这是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;
}
}