时间:2022-12-16 09:31:46 | 栏目:JAVA代码 | 点击:次

授权服务器中 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");
}
}
配置需要资源授权的接口地址
//资源服务配置
@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里配置

# Spring Security Setting security.user.name=bobo security.user.password=xyz
登陆后显示

选择Approve,点击Authorize,会调用回调地址并返回code参数

在获得授权码后,接下去获取访问令牌,访问
http://localhost:8080/oauth/token?code=ghN0hF&grant_type=authorization_code&redirect_uri=http://localhost:9001/callback&scope=read_userinfo

注意:需要在headers里添加认证

认证参数就是授权服务器配置的client和secret

获取token后访问
http://localhost:8080/api/userinfo?access_token=f4345f3a-34a3-4887-bc02-e95150c54bf4

如果token错误,则

授权码模式是最常见的一种授权模式,在oauth2.0内是最安全和最完善的。适用于所有有Server端的应用,如Web站点、有Server端的手机客户端。可以得到较长期限授权。

@Configuration
@EnableAuthorizationServer
public class OAuth2AuthoriationServer extends AuthorizationServerConfigurerAdapter{
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("clientapp")
.secret("112233")
.accessTokenValiditySeconds(60)
.redirectUris("http://localhost:9001/callback")
.authorizedGrantTypes("implicit")
.scopes("admin", "visitor");
}
}
申请授权token,参数和申请授权码类似,client_id,redirect_uri回调地址,response_type有变动,改为直接获取token,scope权限,state用于认证标记,传过去什么回调时传回来什么
http://localhost:8080/oauth/authorize?client_id=clientapp&redirect_uri=http://localhost:9001/callback&response_type=token&scope=admin&state=abc

操作同上,输入密码跳转认证确认,选Approve后点Authorize,跳转

3. 可以看到直接返回了access_token,state也是原样返回
4. 之后按授权码模式第六步操作,把access_token参数带上,进行接口调用就可以了

注意:因为Access token是附着在 redirect_uri 上面被返回的,所以这个 Access token就可能会暴露给资源所有者或者设置内的其它方(对资源所有者来说,可以看到redirect_uri,对其它方来说,可以通过监测浏览器的地址变化来得到 Access token)。

授权服务器配置,需要添加用户认证管理端点authenticationManager,修改模式authorizedGrantTypes为password
// 授权服务器配置
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthoriationServer extends AuthorizationServerConfigurerAdapter{
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("clientapp")
.secret("112233")
.accessTokenValiditySeconds(60)
.redirectUris("http://localhost:9001/callback")
.authorizedGrantTypes("password")
.scopes("admin", "visitor");
}
}
调用以下链接,向客户端和服务器提供用户名密码
http://localhost:8080/oauth/token?password=123456&grant_type=password&username=lll&scope=admin
注意:和授权码模式一样,需要在headers里添加认证
结果:

获取token后,步骤同1.1和1.2模式

只需修改授权服务器,authorizedGrantTypes类型client_credentials
http://localhost:8080/oauth/token?grant_type=client_credentials&scope=admin

可以看到客户端凭证模式也需要在header里添加认证账户密码
获得token后操作同上

以授权码模式为例,步骤同授权码模式,取得授权码后,去取token时,返回

在token过期后,调用
http://localhost:8080/oauth/token?grant_type=refresh_token&refresh_token=ad3941d1-c6dd-4a2e-a9c8-eac6a9a59dd2

返回

就可以拿新的access_token继续调用了建议将access_token和refresh_token的过期时间保存下来,每次调用平台方的业务api前先对access_token和refresh_token进行一下时间判断,如果过期则执行刷新access_token或重新授权操作。refersh_token如果过期就只能让用户重新授权。