基于授权码模式+Spring Security OAuth2的最简授权服务器

就讲操作

基于授权码模式+Spring Security OAuth2的最简授权服务器_第1张图片

@Controller
@ResponseBody
public class UserController {

	// 资源API
    @RequestMapping("/api/userinfo")
    public  String  getUserInfo() {
        User user = (User) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();
        return "已经请求到资源了,用户名:"+user.getUsername();
    }

}
-----------------------------------------------------


//授权服务器配置
@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/**");
    }

}
@SpringBootApplication
public class AuthCodeServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(AuthCodeServerApplication.class, args);
	}
}




-----------------------------------------------------

application.properties

# Spring Security Setting
security.user.name=bobo
security.user.password=xyz
-----------------------------------------------------
POm



	4.0.0

	io.spring2go
	authcode-server
	0.0.1-SNAPSHOT
	jar

	authcode-server
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.10.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-security
		
		
			org.springframework.boot
			spring-boot-starter-web
		

		
		
			org.springframework.security.oauth
			spring-security-oauth2
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
			org.springframework.security
			spring-security-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



 

 操作方式

启动程序

 1. 获取授权码

浏览器请求:

http://localhost:8080/oauth/authorize?client_id=clientapp&redirect_uri=http://localhost:9001/callback&response_type=code&scope=read_userinfo

 基于授权码模式+Spring Security OAuth2的最简授权服务器_第2张图片

案例响应

http://localhost:9001/callback?code=86OWsb

 2. 获取访问令牌

http://localhost:8080/oauth/token?code=86OWsb&grant_type=authorization_code&redirect_uri=http://localhost:9001/callback&scope=read_userinfo

基于授权码模式+Spring Security OAuth2的最简授权服务器_第3张图片

基于授权码模式+Spring Security OAuth2的最简授权服务器_第4张图片

基于授权码模式+Spring Security OAuth2的最简授权服务器_第5张图片

 

curl -X POST --user clientapp:112233

http://localhost:8080/oauth/token

-H
"content-type: application/x-www-form-urlencoded"

-d
"code=86OWsb&

grant_type=authorization_code&

redirect_uri=http://localhost:9001/callback&

scope=read_userinfo"

案例响应:
{
    "access_token": "36cded80-b6f5-43b7-bdfc-594788a24530",
    "token_type": "bearer",
    "expires_in": 43199,
    "scope": "read_userinfo"
}
 


3. 调用API

curl -X GET http://localhost:8080/api/userinfo -H "authorization: Bearer 36cded80-b6f5-43b7-bdfc-594788a24530"

案例响应:

 基于授权码模式+Spring Security OAuth2的最简授权服务器_第6张图片

你可能感兴趣的:(OAuth2)