内容简介:Spring Cloud Security系列教程一:入门
本篇有一定的学习曲线,建议先花一点时间了解一下前置知识:
- Spring Security: http://docs.spring.io/spring-security/site/docs/4.2.2.RELEASE/reference/htmlsingle/
- OAuth2( 重点 ),参考文档: http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html
- Spring Security OAuth2,参考文档: http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#boot-features-security-oauth2
熟悉以上前置知识后,我们进入Spring Cloud Security的学习。在本例中,我们来使用GitHub的账号和密码来登录我们编写的应用。
准备工作
(1) 前往 https://github.com/settings/developers ,点击“Register a new application”按钮,添加一个应用。点击按钮后,界面如下图所示。
(2) 点击“Register application”按钮,即可出现如下图的界面。
记住这边的Client ID以及Client Secret,后面有用。
至此,准备工作就完成了。
编码
在这里,我们正式进行编码。
(1) 创建项目,并添加以下依赖:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-oauth2</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-security</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
即:为应用添加spring-cloud-starter-oauth2、spring-cloud-starter-security两个依赖。
(2) 编写启动类:
@SpringBootApplication
@RestController
public classSecurityApplication{
publicstaticvoidmain(String[] args){
SpringApplication.run(SecurityApplication.class, args);
}
@GetMapping("/welcome")
publicStringwelcome(){
return "welcome";
}
@RequestMapping("/user")
publicPrincipaluser(Principal user){
return user;
}
@Component
@EnableOAuth2Sso // 实现基于OAuth2的单点登录,建议跟踪进代码阅读以下该注解的注释,很有用
public static classSecurityConfigurationextendsWebSecurityConfigurerAdapter{
@Override
publicvoidconfigure(HttpSecurity http)throwsException{
http.
antMatcher("/**")
// 所有请求都得经过认证和授权
.authorizeRequests().anyRequest().authenticated()
.and().authorizeRequests().antMatchers("/","/anon").permitAll()
.and()
// 这里之所以要禁用csrf,是为了方便。
// 否则,退出链接必须要发送一个post请求,请求还得带csrf token
// 那样我还得写一个界面,发送post请求
.csrf().disable()
// 退出的URL是/logout
.logout().logoutUrl("/logout").permitAll()
// 退出成功后,跳转到/路径。
.logoutSuccessUrl("/");
}
}
}
如代码所示,在这里,我们使用 @EnableOAuth2Sso 注解,启用了“基于OAuth2的单点登录”,做了一些安全配置;同时,还定义了两个端点, /welcome 端点返回“welcome”字符串, /user 端点返回当前登录用户的认证信息。
这里说明一下, @EnableOAuth2Sso 注解。如果 WebSecurityConfigurerAdapter 类上注释了 @EnableOAuth2Sso 注解,那么将会添加身份验证过滤器和身份验证入口。
- 如果只有一个
@EnableOAuth2Sso注解没有编写在WebSecurityConfigurerAdapter上,那么它将会为所有路径启用安全,并且会在基于HTTP Basic认证的安全链之前被添加。 - 详见
@EnableOAuth2Sso的注释。
(3) 编写配置文件
server: port: 8080 security: user: password: user # 直接登录时的密码 ignored: / sessions: never # session策略 oauth2: sso: loginPath: /login # 登录路径 client: clientId: 你的clientId clientSecret: 你的clientSecret accessTokenUri: https://github.com/login/oauth/access_token userAuthorizationUri: https://github.com/login/oauth/authorize resource: userInfoUri: https://api.github.com/user preferTokenInfo: false
配置文件中的地址可参考该文档来配置: https://developer.github.com/v3/oauth/ 。
这样,代码就编写完成了。
测试
(1) 启动应用。
(2) 访问 http://localhost:8080/login ,将会跳转到GitHub,进行认证。
(3) 认证通过后,访问 http://localhost:8080/user ,可看到当前用户的信息。
(4) 访问 http://localhost:8080/logout ,可正常退出应用。
配套代码
(1) https://github.com/itmuch/spring-cloud-security-samples/tree/master/security-1
(2) http://git.oschina.net/itmuch/spring-cloud-security-samples/tree/master/security-1
版权说明
本文采用 CC BY 3.0 CN协议 进行许可。 可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。
以上所述就是小编给大家介绍的《Spring Cloud Security系列教程一:入门》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
加密与解密(第4版)
段钢 / 电子工业出版社 / 2018-10-1 / 198
《加密与解密(第4版)》以加密与解密为切入点,讲述了软件安全领域的基础知识和技能,如调试技能、逆向分析、加密保护、外壳开发、虚拟机设计等。这些知识彼此联系,读者在掌握这些内容之后,很容易就能在漏洞分析、安全编程、病毒分析、软件保护等领域进行扩展。从就业的角度来说,掌握加密与解密的相关技术,可以提高自身的竞争能力;从个人成长的角度来说,研究软件安全技术有助于掌握一些系统底层知识,是提升职业技能的重要......一起来看看 《加密与解密(第4版)》 这本书的介绍吧!
HTML 压缩/解压工具
在线压缩/解压 HTML 代码
随机密码生成器
多种字符组合密码