authorization-server 为认证服务器 端口为8080,client 为客户端 端口为8081,resource-server 为资源服务器 端口为8082。
授权的流程为 http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html
密码模式的分支为 password,授权码模式的分支为 authorization-code,简化模式的分支为 implicit,客户端模式的分支为 client-credentials。
以下以最复杂的授权码模式为例:
直接获取资源显示没有资源,点击获取token,此时触发:
用户输入用户名,密码登陆后点击授权。本例中用户名为admin 密码为123,配置在了内存中
String userMsg = "clientApp:secret";
String base64UserMsg = Base64.getEncoder().encodeToString(userMsg.getBytes());
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.add("Authorization", "Basic " + base64UserMsg);
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("client_id", "clientApp");
params.add("grant_type", "authorization_code");
params.add("code", code);
params.add("scope", "read_profile");
params.add("redirect_uri", "http://localhost:8081/api/profile");
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
ResponseEntity<String> response = restTemplate.postForEntity("http://localhost:8080/oauth/token", requestEntity, String.class);
String token = response.getBody();
cline_id和秘钥在header的Authorization中
已使用jwt生成了token
@Bean
public ApprovalStore approvalStore() {
TokenApprovalStore store = new TokenApprovalStore();
store.setTokenStore(tokenStore());
return store;
}
@Bean
public TokenStore tokenStore() {
return new JwtTokenStore(jwtAccessTokenConverter());
}
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
jwtAccessTokenConverter.setSigningKey("secret");
return jwtAccessTokenConverter;
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
endpoints.tokenStore(tokenStore())
.authenticationManager(authenticationManager)
.accessTokenConverter(jwtAccessTokenConverter());
}
访问 http://localhost:8081/get-resource.html 输入第一步获得的token