Cat*_*t H 8 java spring spring-mvc spring-security spring-oauth2
我尝试修改现有的示例 - Tonr2和Sparklr2.我还根据Spring Boot Spring Boot OAuth2查看了本教程.我尝试在Tonr2示例中构建应用程序但没有首次登录(在tonr2上).我只需要在Sparklr2端进行一次身份验证.我这样做:
@Bean
public OAuth2ProtectedResourceDetails sparklr() {
AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails();
details.setId("sparklr/tonr");
details.setClientId("tonr");
details.setTokenName("oauth_token");
details.setClientSecret("secret");
details.setAccessTokenUri(accessTokenUri);
details.setUserAuthorizationUri(userAuthorizationUri);
details.setScope(Arrays.asList("openid"));
details.setGrantType("client_credentials");
details.setAuthenticationScheme(AuthenticationScheme.none);
details.setClientAuthenticationScheme(AuthenticationScheme.none);
return details;
}
Run Code Online (Sandbox Code Playgroud)
但我有Authentication is required to obtain an access token (anonymous not allowed).我查了这个问题.当然,我的用户是匿名的 - 我想登录Sparklr2.此外,我尝试了这种bean的不同设置组合,但没有什么好处.怎么解决?如何使它按我想要的方式工作?
该职位迟到了近两年。
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth instanceof AnonymousAuthenticationToken) {
if (!resource.isClientOnly()) {
throw new InsufficientAuthenticationException(
"Authentication is required to obtain an access token (anonymous not allowed)");
}
}
Run Code Online (Sandbox Code Playgroud)
你要么
ClientCredentialsResourceDetails您的OAuth2RestTemplate, 或AuthorizationCodeResourceDetails在使用访问外部资源之前对用户进行身份验证事实上,在这个tonr2 and sparklr2例子中(我个人觉得这个名字很混乱),要访问 上的资源sparklr2,用户必须首先在 上进行身份验证tonr2。如oauth2/tonr中所示:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("marissa").password("wombat").roles("USER").and().withUser("sam")
.password("kangaroo").roles("USER");
}
Run Code Online (Sandbox Code Playgroud)
如果您的用户是匿名的,您可能需要检查Single Sign On。
对于只想快速尝试 Oauth2 集成的用户,请将基本身份验证添加到您的应用程序中:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated().and().httpBasic();
}
Run Code Online (Sandbox Code Playgroud)
应用程序属性:
spring.security.user.password=password
spring.security.user.name=user
Run Code Online (Sandbox Code Playgroud)
不要忘记添加spring-boot-starter-security到您的项目中。
例如在 gradle 中: compile 'org.springframework.boot:spring-boot-starter-security'
或者您也可以AnonymousAuthenticationToken通过以下方式禁用创建:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.anonymous().disable();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2760 次 |
| 最近记录: |