(1)spring-security-oauth2 从2.4.x版本开始,@EnableAuthorizationServer注解就弃用过时了
(2)当前演示Demo版本:springboot的1.5.x版本与spring-security-oauth2的2.3.8.RELEASE整合,如果使用springboot 2.x.x版本是不兼容的,程序会报错。
(3)spring-security-oauth2 的2.3.8.RELEASE之后的版本与springboot 2.x.x的版本整合写法待学习。
/**
* 用户信息实体
* @Author fenglm
*/
@Data
public class UserInfo {
private String name;
private String email;
}
/**
* 用户信息Controller
* @Author fenglm
*/
@Controller
public class UserController {
/**
* 获取用户信息(资源API)
* @return
*/
@RequestMapping("/api/userinfo")
public ResponseEntity getUserInfo() {
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String email = user.getUsername()+"@fenglm.com";
UserInfo userInfo = new UserInfo();
userInfo.setName(user.getUsername());
userInfo.setEmail(email);
return ResponseEntity.ok(userInfo);
}
}
/**
* 授权服务器配置
* 说明:
* (1)org.springframework.security.oauth从2.4.x版本开始,@EnableAuthorizationServer等注解就弃用过时了,当前Demo使用的是2.3.8.RELEASE版本
* (2)springboot版本:1.5.x 与 security.oauth版本:2.3.8.RELEASE 相对应整合,使用springboot 2.x.x版本是不兼容的
* (3)2.3.8.RELEASE之后的版本、springboot 2.x.x的版本整合写法待学习
*/
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServer extends AuthorizationServerConfigurerAdapter {
@Override
public void configure(ClientDetailsServiceConfigurer clientDetailsServiceConfigurer) throws Exception {
clientDetailsServiceConfigurer.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/**");
}
}
# Spring Security Setting
security.user.name=fenglm
security.user.password=sy123
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.10.RELEASE
com.fenglm.server
authcode-server
1.0.0-SNAPSHOT
authcode-server
基于授权码模式+Spring Security OAuth2的最简授权服务器
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.projectlombok
lombok
org.springframework.boot
spring-boot-starter-security
org.springframework.boot
spring-boot-starter-web
org.springframework.security.oauth
spring-security-oauth2
2.3.8.RELEASE
org.springframework.boot
spring-boot-starter-test
test
org.springframework.security
spring-security-test
5.6.2
test
org.springframework.boot
spring-boot-maven-plugin
注:链接地址里的client_id注意需要跟后台代码里写的一致 点击获取授权码-浏览器请求(注:state参数暂忽略)https://link.zhihu.com/?target=http%3A//localhost%3A8080/oauth/authorize%3Fclient_id%3Dclientapp%26redirect_uri%3Dhttp%3A//localhost%3A9001/callback%26response_type%3Dcode%26scope%3Dread_userinfo
获取授权码-浏览器响应:http://localhost:9001/callback?code=8uYpdo
curl -X POST --user clientapp:112233 http://localhost:8080/oauth/token -H
"content-type: application/x-www-form-urlencoded" -d
"code=8uYpdo&grant_type=authorization_code&redirect_uri=http%3A%2F%2Flocalh
ost%3A9001%2Fcallback&scope=read_userinfo"
{
"access_token": "36cded80-b6f5-43b7-bdfc-594788a24530",
"token_type": "bearer",
"expires_in": 43199,
"scope": "read_userinfo"
}
curl -X GET http://localhost:8080/api/userinfo -H "authorization: Bearer 36cded80-b6f5-43b7-bdfc-594788a24530"
{
"name": "fenglm",
"email": "[email protected]"
}
想要了解更多实用小干货
可关注我的【知乎】