小编Joa*_*nck的帖子

如何使用OAuth2RestTemplate?

我试图了解如何使用OAuth2RestTemplate对象来使用我的OAuth2安全REST服务(在不同的项目下运行,让我们假设在不同的服务器等...)

我的休息服务是:

http://localhost:8082/app/helloworld
Run Code Online (Sandbox Code Playgroud)

- >访问此URL会生成错误,因为我未经过身份验证

要请求令牌,我会去:

http://localhost:8082/app/oauth/token?grant_type=password&client_id=restapp&client_secret=restapp&username=**USERNAME**&password=**PASSWORD**
Run Code Online (Sandbox Code Playgroud)

收到令牌后,我可以使用以下URL连接到REST API(插入示例令牌)

http://localhost:8082/app/helloworld/?access_token=**4855f557-c6ee-43b7-8617-c24591965206**
Run Code Online (Sandbox Code Playgroud)

现在我的问题是如何实现可以使用这个OAuth2安全REST API的第二个应用程序?我真的没有找到任何提供用户名和密码的工作示例(例如来自登录表单),然后生成一个令牌,可以重新使用该令牌从REST API获取数据.

我目前尝试使用以下对象:

BaseOAuth2ProtectedResourceDetails baseOAuth2ProtectedResourceDetails =  new BaseOAuth2ProtectedResourceDetails();
baseOAuth2ProtectedResourceDetails.setClientId("restapp");
baseOAuth2ProtectedResourceDetails.setClientSecret("restapp");
baseOAuth2ProtectedResourceDetails.setGrantType("password");
// how to set user name and password ???

DefaultAccessTokenRequest accessTokenRequest = new DefaultAccessTokenRequest();
OAuth2ClientContext oAuth2ClientContext = new DefaultOAuth2ClientContext(accessTokenRequest());

OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(baseOAuth2ProtectedResourceDetails,oAuth2ClientContext);
Run Code Online (Sandbox Code Playgroud)

但这只是不起作用:(

非常感谢任何想法或非常感谢链接到工作示例和教程.

api rest spring oauth spring-security

37
推荐指数
2
解决办法
9万
查看次数

无法在Spring Security中获取@Secured Method Security注释

我已经做了很多研究,对我而言,一切看起来都很正确......但是我无法让它发挥作用!任何人有任何想法?

无论我做什么,相关的映射仍然公开给任何人(匿名或登录,无论他们有什么角色).

理想情况下,我希望所有请求都是公共的,除了那些由@Secured()注释的请求 - 显然只有具有特定角色的用户才能访问这些映射.

那可能吗?

仅供参考我作为一种解决方法我目前构建了一个方法"hasRole(String role)",它检查登录用户的角色,如果方法返回false,则抛出NotAuthorizedException(自定义).

的UserDetails

  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {

      List<GrantedAuthority> grantedAuthorities = null;

      System.out.print("Account role... ");
      System.out.println(account.getRole());

      if (account.getRole().equals("USER")) {
          GrantedAuthority grantedAuthority = new SimpleGrantedAuthority("ROLE_USER");
          grantedAuthorities = Arrays.asList(grantedAuthority);
      }

      if (account.getRole().equals("ADMIN")) {
          GrantedAuthority grantedAuthorityUser = new SimpleGrantedAuthority("ROLE_USER");
          GrantedAuthority grantedAuthorityAdmin = new SimpleGrantedAuthority("ROLE_ADMIN");
          grantedAuthorities = Arrays.asList(grantedAuthorityUser, grantedAuthorityAdmin);
      }

      return grantedAuthorities;
  }
Run Code Online (Sandbox Code Playgroud)

SecurityConfig

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthFailure authFailure;

    @Autowired
    private AuthSuccess authSuccess;

    @Autowired
    private EntryPointUnauthorizedHandler …
Run Code Online (Sandbox Code Playgroud)

java spring annotations spring-security

2
推荐指数
1
解决办法
3288
查看次数

标签 统计

spring ×2

spring-security ×2

annotations ×1

api ×1

java ×1

oauth ×1

rest ×1