小编BOS*_*OSS的帖子

使用Spring oauth2使用OAuth安全的REST Web服务

我想从服务器使用REST Web服务,该服务器使用oauth2保护其资源.

我使用Spring boot(JHipster).

要做到这一点,我在SecurityConfiguration课堂上这样:

@Value("${oauth.resource:http://sercverUsingOAuth2}")
private String baseUrl;

@Value("${oauth.authorize:http://sercverUsingOAuth2/rest/oauth/token}")
private String authorizeUrl;

@Value("${oauth.token:http://sercverUsingOAuth2/rest/oauth/token}")
private String tokenUrl;

@Bean
public OAuth2RestOperations oauth2RestTemplate() {
    AccessTokenRequest atr = new DefaultAccessTokenRequest();
    return new OAuth2RestTemplate(resource(),
            new DefaultOAuth2ClientContext(atr));
}

@Bean
protected OAuth2ProtectedResourceDetails resource() {
    AuthorizationCodeResourceDetails resource = new AuthorizationCodeResourceDetails();
    resource.setAccessTokenUri(tokenUrl);
    resource.setUserAuthorizationUri(authorizeUrl);
    resource.setClientId("client_id");
    resource.setClientSecret("client_secret");
    resource.setGrantType("grant_type");
    return resource;
}
Run Code Online (Sandbox Code Playgroud)

这个class(SecurityConfiguration)使用以下方法进行注释:

@Configuration
@EnableWebSecurity
@EnableOAuth2Client
Run Code Online (Sandbox Code Playgroud)

这是我的controller(Spring MVC):

@RestController
@RequestMapping("/consume")
public class MyContrtoller {

@Inject
private OAuth2RestOperations oauth2RestTemplate;

@RequestMapping(value = "/oauth2", method = RequestMethod.GET, produces …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc oauth-2.0 spring-boot jhipster

6
推荐指数
1
解决办法
9468
查看次数

标签 统计

jhipster ×1

oauth-2.0 ×1

spring ×1

spring-boot ×1

spring-mvc ×1