最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于JWT實現SSO單點登錄流程圖解

 更新時間:2020年07月01日 10:07:25   作者:明月之詩  
這篇文章主要介紹了基于JWT實現SSO單點登錄流程圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

  一、基于JWT實現SSO單點登錄原理

  1、什么是單點登錄

  所謂單點登錄就是有多個應用部署在不同的服務器上,只需登錄一次就可以互相訪問不同服務器上的資源。

  2、單點登錄流程

  當一個訪問請求發(fā)給應用A,如果這個請求需要登錄以后才能訪問,那么應用A就會向認證服務器請求授權,這時候就把用戶引導到認證服務器上。用戶在認證服務器上完成認證并授權。認證授權完成后,認證服務器返回給應用A一個授權碼,應用A攜帶授權碼到認證服務器請求令牌,認證服務器返回應用A一個JWT,應用A解析JWT里面的信息,完成登錄。這是一個標準的OAuth2的授權碼流程。

  走完認證流程后,給出去的JWT實際上里面包含的就是當前用戶在認證服務器上登錄以后用戶的認證信息,應用A解析JWT后,自己生成一個經過認證的Authentication放到它的SpringSecurity和SecurityContext里面。

  當訪問應用服務器B的時候,同樣引導用戶去認證服務器請求授權(不需要登錄),用戶授權可以用登錄的信息去訪問應用B,后面同樣是授權碼流程,返回JWT給應用B。兩個應用返回不同的JWT,但是解析出的信息是一樣的。

  二、實現單點登錄

  1、父工程(sso-demo)

  1)pom.xml

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-dependencies</artifactId>
      <version>2.0.4.RELEASE</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.security.oauth.boot</groupId>
      <artifactId>spring-security-oauth2-autoconfigure</artifactId>
      <version>2.1.3.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-jwt</artifactId>
      <version>1.0.10.RELEASE</version>
    </dependency>
  </dependencies>
</dependencyManagement>

  2、認證服務(sso-server)

  1)pom.xml

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.security.oauth.boot</groupId>
  <artifactId>spring-security-oauth2-autoconfigure</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-jwt</artifactId>
</dependency>

  2)application.properties

server.port = 9999
server.servlet.context-path = /server

  3)WebSecurityConfig.java

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
  
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.httpBasic().and().csrf().disable();   
  }
  @Bean
  public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
  }
}

  4)MyUserDetailsService.java

@Component
public class MyUserDetailsService implements UserDetailsService{

  @Autowired
  private PasswordEncoder passwordEncoder;
  
  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    System.out.println("登錄用戶名:"+username);
    String password = passwordEncoder.encode("123456");
    return new User(username,password,true,true,true,true,
        AuthorityUtils.commaSeparatedStringToAuthorityList("all"));
  }
}

  5)SsoAuthorizationServerConfig.java

@Configuration
@EnableAuthorizationServer
public class SsoAuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
  
  @Autowired
  private PasswordEncoder passwordEncoder;
  
  @Override
  public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.inMemory()
        .withClient("appclient_1").secret(passwordEncoder.encode("client1_123456"))
        .authorizedGrantTypes("authorization_code","refresh_token")
        .scopes("all")
        .redirectUris("http://127.0.0.1:8080/client1/login")
       .and()
        .withClient("appclient_2").secret(passwordEncoder.encode("client2_123456"))
        .authorizedGrantTypes("authorization_code","refresh_token")
        .scopes("all")
        .redirectUris("http://127.0.0.1:8060/client2/login");
  }
  @Override
  public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    endpoints.tokenStore(jwtTokenStore()).accessTokenConverter(jwtAccessTokenConverter());
  }
  @Override
  public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
    security.tokenKeyAccess("isAuthenticated()");//訪問tokenKey(秘鑰shxiang)的時候需要身份認證 
  }
  @Bean
  public TokenStore jwtTokenStore() {
    return new JwtTokenStore(jwtAccessTokenConverter());
  }
  @Bean
  public JwtAccessTokenConverter jwtAccessTokenConverter() {
    JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
    accessTokenConverter.setSigningKey("shxiang");//設置秘鑰
    return accessTokenConverter;
  }
}

  6)SsoServerApplication.java

@SpringBootApplication
public class SsoServerApplication {
  public static void main(String[] args) {
    SpringApplication.run(SsoServerApplication.class, args);
  }
}

  3、應用1(sso-client1)

  1)pom.xml,同上

  2)application.properties

security.oauth2.client.client-id = appclient_1
security.oauth2.client.client-secret = client1_123456
security.oauth2.client.user-authorization-uri = http://127.0.0.1:9999/server/oauth/authorize

security.oauth2.client.access-token-uri = http://127.0.0.1:9999/server/oauth/token
security.oauth2.resource.jwt.key-uri = http://127.0.0.1:9999/server/oauth/token_key

server.port=8080
server.servlet.context-path =/client1

  3)index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SSO Client1</title>
</head>
<body>
  <h1>SSO Demo Client1</h1>
  <a  rel="external nofollow" >訪問Client2</a>
</body>
</html>

  4)SsoClient1Application.java

@SpringBootApplication
@RestController
@EnableOAuth2Sso
public class SsoClient1Application {
  public static void main(String[] args) {
    SpringApplication.run(SsoClient1Application.class, args);
  }
  @GetMapping("/user")
  public Authentication user(Authentication user) {
    return user;
  }
}

  4、應用2(sso-client2)

  1)pom.xml,同上

  2)application.properties,類比應用1修改

  3)index.html,類比應用1修改

  4)SsoClient2Application.java,同上

  5、測試

  1)瀏覽器輸入:127.0.0.1:8080/client1/index.html

  2)用戶名隨便輸入,密碼輸入123456

  3)點擊Authorize 

  4)點擊超級鏈接訪問Client2

  5)點擊Authorize

  認證成功,后面點擊兩個超級鏈接可以任意訪問,無需登錄 、無需點擊Authorize。

  注意:

  1)雖是同一用戶,但是訪問http://127.0.0.1:8080/client1/user和http://127.0.0.1:8060/client2/user獲取的Token值不一樣。

  2)實現跳過授權,登錄后直接訪問,修改如下代碼:

  3)表單登錄與httpBasic登錄,修改WebSecurityConfig.java中configure方法

httpBasic登錄:http.httpBasic().and().csrf().disable();
表單登錄:http.formLogin().and().authorizeRequests().anyRequest().authenticated();

  4)重點:瀏覽器訪問要用127.0.0.1不要用localhost。要設置應用路徑server.servlet.context-path =/xxxx,不能直接到端口號。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Java實現定時讀取json文件里內容的示例代碼

    Java實現定時讀取json文件里內容的示例代碼

    有時候我們會需要定時來讀取JSON配置文件里的內容,來執(zhí)行一些業(yè)務邏輯上的操作,本文就介紹了Java實現定時讀取json文件里內容的示例代碼,感興趣的可以了解一下
    2023-08-08
  • springboot實現文件上傳步驟解析

    springboot實現文件上傳步驟解析

    這篇文章主要介紹了springboot實現文件上傳步驟解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • Java實現藍橋杯G將軍的示例代碼

    Java實現藍橋杯G將軍的示例代碼

    這篇文章主要介紹了Java實現藍橋杯G將軍的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • SpringBoot項目中引入本地JAR包配置的幾種方法

    SpringBoot項目中引入本地JAR包配置的幾種方法

    SpringBoot有時需要引入本地JAR包以便重用已有的代碼庫或者第三方庫,本文主要介紹了SpringBoot項目中引入本地JAR包配置的幾種方法,具有一定的參考價值,感興趣的可以了解一下
    2024-08-08
  • Java?LocalDateTime常用操作方法

    Java?LocalDateTime常用操作方法

    這篇文章主要介紹了Java?LocalDateTime實用方法,Java8提供了新的時間接口LocalDateTime,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2022-01-01
  • Spring Cloud Gateway 使用JWT工具類做用戶登錄校驗功能

    Spring Cloud Gateway 使用JWT工具類做用戶登錄校驗功能

    這篇文章主要介紹了Spring Cloud Gateway 使用JWT工具類做用戶登錄校驗的示例代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Spring?MVC跨域問題及解決

    Spring?MVC跨域問題及解決

    這篇文章主要介紹了Spring?MVC跨域問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • java使用Filter實現自動登錄的方法

    java使用Filter實現自動登錄的方法

    這篇文章主要為大家詳細介紹了java使用Filter實現自動登錄的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • idea首次使用需要配置哪些東西

    idea首次使用需要配置哪些東西

    這篇文章主要介紹了idea首次使用需要配置哪些東西,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • maven無法依賴spring-cloud-stater-zipkin的解決方案

    maven無法依賴spring-cloud-stater-zipkin的解決方案

    這篇文章主要介紹了maven無法依賴spring-cloud-stater-zipkin如何解決,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-05-05

最新評論

桐柏县| 碌曲县| 万州区| 遵义县| 吉木萨尔县| 定西市| 沧源| 长沙县| 时尚| 思南县| 莆田市| 葫芦岛市| 萨迦县| 天等县| 文水县| 焦作市| 陆良县| 通榆县| 云龙县| 邵阳县| 天峻县| 铜山县| 新邵县| 珠海市| 巴中市| 资兴市| 循化| 仙桃市| 高邑县| 大埔区| 玛多县| 西丰县| 深水埗区| 玛纳斯县| 神池县| 阿克陶县| 渭南市| 新昌县| 镇坪县| 涟源市| 宿州市|