本文單純從簡單的技術實現來講,不涉及開放平臺的多維度的運營理念。
什麼是開放平臺
通過開放自己平臺產品服務的各種API接口,讓其他第三方開發者在開發應用時根據需求直接調用,例如微信登錄、QQ登錄、微信支付、微博登錄、熱門等。
讓第三方應用通過開發平臺,使得自身海量數據資源得到沉澱(變現)
目前國內主流的網站的的開放平臺,都是基於oauth2.0 協議進行做的開放平臺
- 微信開放平臺授權機制流程圖
- 微博開放平臺授權機制流程圖
oauth2.0 授權碼模式
授權碼模式(authorization code)是功能最完整、流程最嚴密的授權模式。 它的特點就是通過客戶端的後臺服務器,與"服務提供商"的認證服務器進行互動,能夠滿足絕大多數開放平臺認證授權的需求。
引入相關依賴
<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>
配置認證服務器
通過內存模式,初始化一個支持授權碼模式的客戶端
@Configuration
@AllArgsConstructor
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Override
@SneakyThrows
public void configure(ClientDetailsServiceConfigurer clients) {
clients.inMemory()
.withClient("pigx") // client_id
.secret("pigx") // client_secret
.authorizedGrantTypes("authorization_code") // 該client允許的授權類型
.scopes("app"); // 允許的授權範圍
}
}
初步完成,測試一下
注意這裏是 /oauth/authorize 不是 /oauth/token 接口,只需要帶 client_id 即可。
localhost:9999/oauth/authorize?client_id=pigx&response_type=code&redirect_uri=https://pig4cloud.com
- 先進行basic 登錄,默認用戶user,密碼已經打在控制檯自己查即可
- 授權確認
- 登錄成功帶着code回調到目標接口
- 通過/oauth/token獲取登錄令牌
簡單的幾步就完成上圖微信或者其他網站的授權流程,不過目前爲止 略顯簡陋
- 登錄沒有界面,用戶密碼數據庫沒有保存
- 確認授權界面太醜,沒有個性化
配置安全登錄
- 配置未登錄攔截重定向到 loginPage
- 配置登錄完成提交的頁面路徑 這裏會被spring security 接管
@Primary
@Order(90)
@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Override
@SneakyThrows
protected void configure(HttpSecurity http) {
http
.formLogin()
.loginPage("/token/login")
.loginProcessingUrl("/token/form")
.and()
.authorizeRequests()
.anyRequest().authenticated();
}
}
認證服務器配置用戶加載規則實現
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
endpoints.userDetailsService(pigxUserDetailsService)
}
// 通過這步去加載數據的用戶名密碼
public interface UserDetailsService {
UserDetails loadUserByUsername(String var1) throws UsernameNotFoundException;
}
重寫原有認證頁面
默認邏輯/oauth/confirm_access,讓他重定向到我們自己的路徑,然後進行個性哈
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
endpoints
.userDetailsService(pigxUserDetailsService)
.pathMapping("/oauth/confirm_access", "/token/confirm_access")
}
獲取上下文中的授權信息,傳給前端
/**
* 確認授權頁面
*
* @param request
* @param session
* @param modelAndView
* @return
*/
@GetMapping("/confirm_access")
public ModelAndView confirm(HttpServletRequest request, HttpSession session, ModelAndView modelAndView) {
Map<String, Object> scopeList = (Map<String, Object>) request.getAttribute("scopes");
modelAndView.addObject("scopeList", scopeList.keySet());
Object auth = session.getAttribute("authorizationRequest");
if (auth != null) {
AuthorizationRequest authorizationRequest = (AuthorizationRequest) auth;
ClientDetails clientDetails = clientDetailsService.loadClientByClientId(authorizationRequest.getClientId());
modelAndView.addObject("app", clientDetails.getAdditionalInformation());
modelAndView.addObject("user", SecurityUtils.getUser());
}
modelAndView.setViewName("ftl/confirm");
return modelAndView;
}
最終效果
- 把用戶頭像等信息展示出來就蠻好看了
總結
- 以上源碼參考個人項目 基於Spring Cloud、OAuth2.0開發基於Vue前後分離的開發平臺
- QQ: 2270033969 一起來聊聊你們是咋用 spring cloud 的吧。
- 歡迎關注我們的公衆號獲得更多的好玩JavaEE 實踐