Spring boot OAuth2 例子
来源:互联网 发布:win10ipv4和ipv6无网络 编辑:程序博客网 时间:2024/06/05 20:24
pom.xml
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><dependency><groupId>org.springframework.security.oauth</groupId><artifactId>spring-security-oauth2</artifactId></dependency>
Controller为 Resource
@RestControllerpublic class HelloController {@RequestMapping("/hello")public String hello() {return "Hello Spring OAuth2";}}
启动入口 HelloOAuthApplication.java
@SpringBootApplicationpublic class HelloOAuth2Application {public static void main(String[] args) {SpringApplication.run(HelloOAuth2Application.class, args);}}
SecurityConfiguration:
@Configuration@EnableGlobalAuthenticationpublic class SecurityConfiguration extends GlobalAuthenticationConfigurerAdapter {@Overridepublic void init(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().withUser("admin").password("123").authorities("ADMIN");}}
OAuth2 Configutation:
@Configuration@EnableAuthorizationServerpublic class OAuth2AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {@Autowiredprivate AuthenticationManager authenticationManager;@Overridepublic void configure(AuthorizationServerSecurityConfigurer security) throws Exception {super.configure(security);}@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {clients.inMemory().withClient("client").secret("secret").authorizedGrantTypes("password", "authorization_code").scopes("myscope");}@Overridepublic void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {endpoints.authenticationManager(authenticationManager);}}
@Configuration@EnableResourceServerpublic class OAuth2ResourceServerConfiguration extends ResourceServerConfigurerAdapter {@Overridepublic 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
- Spring boot OAuth2 例子
- java编程spring boot security oauth2 jwt完美整合例子
- spring boot security oauth2 jwt完美整合例子-java编程
- Spring Boot OAuth2
- spring-boot oauth2
- Spring Boot and OAuth2翻译
- spring Security oAuth2例子分析
- 利用Spring Boot Oauth2 来熟悉oauth2 之
- Spring boot --Oauth2.0 + 单点登录
- spring boot做OAuth2认证服务端
- spring boot security oauth2 jwt 服务端实现
- 使用Spring boot 快速 搭建 OAuth2 Server
- Spring Boot入门例子
- spring boot 简单例子
- spring boot简单例子
- spring boot security oauth2 实现第三方登录
- spring boot + security oauth2 + redis + mongodb 框架搭建
- Spring boot oauth2 研究第N天 action 2
- java串行化技术
- 搜狐,数字删除
- 历史 (并查集 按秩合并)
- CSS3阴影 box-shadow的使用和技巧总结--2017
- C语言入门第十篇,switch语句
- Spring boot OAuth2 例子
- 查找命令(1)——find
- MFC RadioButton用法详解
- I2S音频总线学习(二)I2S总线协议
- [转载]livid 写给他自己的
- DIV+CSS实现电台列表设计
- 高雄试点全台首例LoRa路边智慧停车示范项目
- Hibernate3升级Hibernate4.3后报“HHH000223”
- 机器学习——决策树与随机森林