1. OAuth2简易实战(一)-四种模式
1.1. 授权码授权模式(Authorization code Grant)
1.1.1. 流程图
1.1.2. 授权服务器配置
配置授权服务器中 client,secret,redirectUri,授权模式,权限配置
//授权服务器配置
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServer extends
AuthorizationServerConfigurerAdapter {
@Override
public void configure(ClientDetailsServiceConfigurer clients)
throws Exception {
clients.inMemory()
.withClient("clientapp")
.secret("112233")
.redirectUris("http://localhost:9001/callback")
// 授权码模式
.authorizedGrantTypes("authorization_code")
.scopes("read_userinfo", "read_contacts");
}
}
1.1.3. 资源服务器配置
配置需要资源授权的接口地址
//资源服务配置
@Configuration
@EnableResourceServer
public class OAuth2ResourceServer extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.requestMatchers()
.antMatchers("/api/**");
}
}
1.1.4. 操作步骤
浏览器请求下列地址,获取授权code,请求参数client_id,redirect_uri回调地址,response_type响应类型,scope权限
http://localhost:8080/oauth/authorize?client_id=clientapp&redirect_uri=http://localhost:9001/callback&response_type=code&scope=read_userinfo
输入用户名密码,该密码为Spring Security的登路密码,application.properties里配置