javascript
微服务集成cas_Spring Cloud Security集成CAS (单点登录)对微服务认证
一、前言
由于leader要求在搭好的spring cloud 框架中加入對(duì)微服務(wù)的認(rèn)證包括單點(diǎn)登錄認(rèn)證,來(lái)確保系統(tǒng)的安全,所以研究了Spring Cloud Security這個(gè)組件。在前面搭好的demo中,如何確保微服務(wù)的安全,為整個(gè)系統(tǒng)添加安全控制,就需要用到Spring Cloud Security。用戶通過(guò)服務(wù)網(wǎng)關(guān)zuul來(lái)訪問(wèn)任何一個(gè)微服務(wù)的時(shí)候,都需要跳轉(zhuǎn)到第三方的認(rèn)證比如github或者自己搭好的CAS單點(diǎn)登錄服務(wù),當(dāng)認(rèn)證通過(guò)才能訪問(wèn)對(duì)應(yīng)的服務(wù)。在研究spring cloud security 之前先對(duì)一些概念進(jìn)行了解了。
OAuth2(重點(diǎn)),參考文檔: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
在這個(gè)文章中主要記錄當(dāng)用戶通過(guò)服務(wù)網(wǎng)關(guān)zuul入口訪問(wèn)任何一個(gè)微服務(wù)。需要先跳轉(zhuǎn)到GitHub,使用Github進(jìn)行認(rèn)證,認(rèn)證通過(guò)之后才能跳轉(zhuǎn)到訪問(wèn)我們提供的微服務(wù)。
二、詳細(xì)實(shí)現(xiàn)
2.1 準(zhǔn)備工作
(1) 前往https://github.com/settings/developers,點(diǎn)擊“Register a new application”按鈕,添加一個(gè)應(yīng)用。點(diǎn)擊按鈕后,界面如下圖所示。Homepage URL 和callback url是寫zuul的端口。
(2) 點(diǎn)擊“Register application”按鈕,即可出現(xiàn)如下圖的界面。
記住這邊的Client ID以及Client Secret,后面有用。
至此,準(zhǔn)備工作就完成了。
2.2 編碼
代碼測(cè)試成功之后的Github地址:https://github.com/LoveIpo/spring-cloud-demo/tree/master/Zuul_CAS這個(gè)Zuul_CAS是在zuul中進(jìn)一步完善!
在這里,我們正式進(jìn)行編碼。因?yàn)槲沂窃诜?wù)網(wǎng)關(guān)zuul中添加單點(diǎn)登錄的服務(wù)認(rèn)證授權(quán)。所以對(duì)前面demo中的zuul 工程進(jìn)一步完善。
(1) 在pom.xml文件為應(yīng)用添加spring-cloud-starter-oauth2、spring-cloud-starter-security兩個(gè)依賴。
org.springframework.cloud
spring-cloud-starter-zuul
org.springframework.cloud
spring-cloud-starter
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-oauth2
org.springframework.cloud
spring-cloud-starter-security
(2) 在zuul的啟動(dòng)類中添加如下代碼
@SpringBootApplication
@EnableZuulProxy
@RestController
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
@GetMapping("/")
public String welcome() {
return "welcome";
}
@RequestMapping("/user")
public Principal user(Principal user) {
return user;
}
@Component
@EnableOAuth2Sso // 實(shí)現(xiàn)基于OAuth2的單點(diǎn)登錄,建議跟蹤進(jìn)代碼閱讀以下該注解的注釋,很有用
public static class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.
antMatcher("/**")
// 所有請(qǐng)求都得經(jīng)過(guò)認(rèn)證和授權(quán)
.authorizeRequests().anyRequest().authenticated()
.and().authorizeRequests().antMatchers("/","/anon").permitAll()
.and()
// 這里之所以要禁用csrf,是為了方便。
// 否則,退出鏈接必須要發(fā)送一個(gè)post請(qǐng)求,請(qǐng)求還得帶csrf token
// 那樣我還得寫一個(gè)界面,發(fā)送post請(qǐng)求
.csrf().disable()
// 退出的URL是/logout
.logout().logoutUrl("/logout").permitAll()
// 退出成功后,跳轉(zhuǎn)到/路徑。
.logoutSuccessUrl("/login");
}
}
}
如代碼所示,在這里,我們使用@EnableOAuth2Sso 注解,啟用了“基于OAuth2的單點(diǎn)登錄”,做了一些安全配置;同時(shí),還定義了兩個(gè)端點(diǎn),/ 端點(diǎn)返回“welcome”字符串,/user 端點(diǎn)返回當(dāng)前登錄用戶的認(rèn)證信息。
這里說(shuō)明一下,@EnableOAuth2Sso注解。如果WebSecurityConfigurerAdapter類上注釋了@EnableOAuth2Sso注解,那么將會(huì)添加身份驗(yàn)證過(guò)濾器和身份驗(yàn)證入口。
如果只有一個(gè)@EnableOAuth2Sso注解沒(méi)有編寫在WebSecurityConfigurerAdapter上,那么它將會(huì)為所有路徑啟用安全,并且會(huì)在基于HTTP Basic認(rèn)證的安全鏈之前被添加。詳見(jiàn)@EnableOAuth2Sso的注釋。
(3) 修改zuul 的application.yml文件,部分代碼如下
server:
port: 7073
security:
user:
password: user # 直接登錄時(shí)的密碼
ignored: /
sessions: never # session策略
oauth2:sso:loginPath:/login# 登錄路徑
client:clientId:你的clientIdclientSecret:你的clientSecretaccessTokenUri:https://github.com/login/oauth/access_tokenuserAuthorizationUri:https://github.com/login/oauth/authorizeresource:userInfoUri:https://api.github.com/userpreferTokenInfo:false
spring:
application:
name: zuul
eureka:
client:
serviceUrl:
defaultZone: http://localhost:7071/eureka/
這樣,通過(guò)服務(wù)網(wǎng)關(guān)zuul來(lái)訪問(wèn)任何一個(gè)服務(wù)都要跳轉(zhuǎn)到github進(jìn)行認(rèn)證的主要代碼就編寫完成了。
2.3 測(cè)試
(1) 啟動(dòng)Eureka、zuul、serviceA
(2) 當(dāng)通過(guò)服務(wù)網(wǎng)關(guān)zuul(端口7073) 訪問(wèn)serviceA 的url:http://localhost:7073/api-a/add?a=111&b=113時(shí)。頁(yè)面會(huì)自動(dòng)跳轉(zhuǎn)到github進(jìn)行認(rèn)證。你也可以通過(guò)zuul訪問(wèn)serviceB也會(huì)自動(dòng)跳轉(zhuǎn)到github進(jìn)行認(rèn)證之后才能回調(diào)到serviceB。
(3) 當(dāng)輸入github的用戶名和密碼認(rèn)證通過(guò)之后,會(huì)出現(xiàn)serviceA的調(diào)用結(jié)果。如下圖所示
(4) 當(dāng)你認(rèn)證通過(guò)之后輸入http://localhost:7073/user?可以看到你github 的用戶信息。
總結(jié)
以上是生活随笔為你收集整理的微服务集成cas_Spring Cloud Security集成CAS (单点登录)对微服务认证的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: js opacity0点击_javasc
- 下一篇: fguillot json rpc_Hy