java oauth2 例子_Spring boot OAuth2 例子

pom.xml

org.springframework.boot

spring-boot-starter-security

org.springframework.security.oauth

spring-security-oauth2

Controller为 Resource

@RestController

public class HelloController {

@RequestMapping("/hello")

public String hello() {

return "Hello Spring OAuth2";

}

}

启动入口 HelloOAuthApplication.java

@SpringBootApplication

public class HelloOAuth2Application {

public static void main(String[] args) {

SpringApplication.run(HelloOAuth2Application.class, args);

}

}

SecurityConfiguration:

@Configuration

@EnableGlobalAuthentication

public class SecurityConfiguration extends GlobalAuthenticationConfigurerAdapter {

@Override

public void init(AuthenticationManagerBuilder auth) throws Exception {

auth.inMemoryAuthentication().withUser("admin").password("123").authorities("ADMIN");

}

}

OAuth2 Configutation:

@Configuration

@EnableAuthorizationServer

public class OAuth2AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

@Autowired

private AuthenticationManager authenticationManager;

@Override

public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {

super.configure(security);

}

@Override

public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

clients.inMemory()

.withClient("client")

.secret("secret")

.authorizedGrantTypes("password", "authorization_code")

.scopes("myscope");

}

@Override

public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

endpoints.authenticationManager(authenticationManager);

}

}

@Configuration

@EnableResourceServer

public class OAuth2ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

@Override

public void configure(HttpSecurity http) throws Exception {

http.authorizeRequests()

.antMatchers("/").permitAll()

.antMatchers("/oauth/*").permitAll()

.antMatchers("/hello").authenticated();

}

}

启动,

C:\Users\ahan>curl -i -u client:secret http://localhost:8080/oauth/token -d "grant_type=password&scope=myscope&username=admin&password=123"

HTTP/1.1 200

X-Content-Type-Options: nosniff

X-XSS-Protection: 1; mode=block

Cache-Control: no-cache, no-store, max-age=0, must-revalidate

Pragma: no-cache

Expires: 0

X-Frame-Options: DENY

Content-Type: application/json;charset=UTF-8

Transfer-Encoding: chunked

Date: Mon, 28 Aug 2017 08:00:43 GMT

{"access_token":"e9d51cd6-ed63-40e6-b3b8-7b5c09f5b451","token_type":"bearer","expires_in":42711,"scope":"myscope"}

根据上面得到的token,访问Resource

C:\Users\ahan>curl -i -H "Authorization: bearer e9d51cd6-ed63-40e6-b3b8-7b5c09f5b451" localhost:8080/hello

HTTP/1.1 200

X-Content-Type-Options: nosniff

X-XSS-Protection: 1; mode=block

Cache-Control: no-cache, no-store, max-age=0, must-revalidate

Pragma: no-cache

Expires: 0

X-Frame-Options: DENY

Content-Type: text/plain;charset=UTF-8

Content-Length: 19

Date: Mon, 28 Aug 2017 08:02:30 GMT

Hello Spring OAuth2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值