<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      springboot2.x實現oauth2授權碼登陸

      一 進行授權頁

       
      二 使用資源站用戶登陸
      自動跨到資源登陸頁,先登陸
       

      三 授權資源類型

      登陸成功后,去授權你的資源,這些資源是在AuthorizationServerConfig.configure方法里配置的
      @Override
      public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
      clients.inMemory()
      .withClient(ClientID)
      .secret(passwordEncoder.encode(ClientSecret))
      .authorizedGrantTypes("authorization_code", "refresh_token",
      "password", "implicit")
      .scopes("read","write","del","userinfo")
      .redirectUris(RedirectURLs);
      }

      四 接到code

      授權之后,系統會重定向到你的redirect_uri這個頁面,并帶上唯一的code

      五 獲取access_token

      我們拿著code就要再去授權服務器去獲取token了,你可以在你的代碼里寫這個,也可以手動拿著code,去拼成一個url,再去拿token,就像這下面的實例。
      注意向oauth/token發的是post請求,client_id和client_secret如果在url上傳遞,如果在AuthorizationServerConfig類的configure方法中開啟allowFormAuthenticationForClients,代碼如下
      @Override
      public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
      oauthServer.tokenKeyAccess("isAuthenticated()")
      .checkTokenAccess("permitAll()")
      .allowFormAuthenticationForClients();//支持把secret和clientid寫在url上,否則需要在頭上
      }
      然后請求后給有下面的響應
      Authorization Ccode------RFRLFY
      access_token_url http://localhost:8081/oauth/token?client_id=android1&code=RFRLFY&grant_type=authorization_code&redirect_uri=http://localhost:8081/callback&client_secret=android1
      Access Token Response ---------{"access_token":"faadf3bf-6488-4036-bc3b-21b0a979602c","token_type":"bearer","refresh_token":"1b01f133-c5ab-419f-8125-088c85916ecb","expires_in":43187,"scope":"read"}

      回調頁面代碼,主要實現了對code的獲取,對access_token的組織,然后請求時把access_token帶上,這個方法一般會做成公用的過濾器

      @Controller
      public class UserController {
        @RequestMapping(value = "/callback", method = RequestMethod.GET)
        public ResponseEntity<String> callback(@RequestParam("code") String code) throws JsonProcessingException, IOException {
          ResponseEntity<String> response = null;
          System.out.println("Authorization Ccode------" + code);
          RestTemplate restTemplate = new RestTemplate();
          String access_token_url = "http://localhost:8081/oauth/token";
          access_token_url += "?client_id=android1&code=" + code;
          access_token_url += "&grant_type=authorization_code";
          access_token_url += "&redirect_uri=http://localhost:8081/callback";
          access_token_url += "&client_secret=android1";
          System.out.println("access_token_url " + access_token_url);
          response = restTemplate.exchange(access_token_url, HttpMethod.POST, null, String.class);
          ObjectMapper mapper = new ObjectMapper();
          JsonNode node = mapper.readTree(response.getBody());
          String token = node.path("access_token").asText(); System.out.println("access_token" +access_token);
          String url = "http://localhost:8081/index"; HttpHeaders headers1 = new HttpHeaders(); headers1.add("Authorization", "Bearer " + token); HttpEntity<String> entity = new HttpEntity<>(headers1); ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); return result; }

      六 拿著access_token去請求具體的資源

      可以在url地址上直接:http://localhost:8081/index?access_token=faadf3bf-6488-4036-bc3b-21b0a979602c
      七 如何開啟oauth scopes授權
      .access("#oauth2.hasScope('del')") 這個需要在ResourceServerConfig.configure里添加它,例如下載代碼
      @Configuration
      @EnableResourceServer
      @Order(6)
      public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
      @Override
      public void configure(HttpSecurity http) throws Exception {
      http.csrf().disable()//禁用了 csrf 功能
      .authorizeRequests()//限定簽名成功的請求
      .antMatchers("/index").access("#oauth2.hasScope('del')") //授權碼scopes里需要選中del才可以訪問
      .antMatchers("/user").authenticated()//簽名成功后可訪問,不受role限制
      .anyRequest().permitAll()//其他沒有限定的請求,允許訪問
      .and().anonymous()//對于沒有配置權限的其他請求允許匿名訪問
      .and().formLogin()//使用 spring security 默認登錄頁面
      .and().httpBasic();//啟用http 基礎驗證
       
      }
      }

      八  需要注意的地方

      如果你對用戶進行了角色和權限的配置,對于某些保護接口需要有指定權限才能訪問的話,需要重getAuthorities方法,否則,你的權限將會失效!

      @Entity
      @Data
      public class User extends BaseEntity implements UserDetails {
        @Id
        @GeneratedValue
        private Long id;
        private String username;
        private String password;
        private String firstName;
        private String lastName;
        @Email
        private String email;
        private String imageUrl;
      
      
        @JsonIgnore
        @ManyToMany(targetEntity = Role.class, fetch = FetchType.EAGER)
        @BatchSize(size = 20)
        private Set<Role> roles = new HashSet<>();
      
        @Transient
        private Set<GrantedAuthority> authorities = new HashSet<>();
      
        /**
         * 注意,這塊需要加@Override重寫,否則權限無效.
         *
         * @return
         */
        @Override
        public Set<GrantedAuthority> getAuthorities() {
          Set<GrantedAuthority> authorities = new HashSet<>();
          for (Role role : this.roles) {
            for (Authority authority : role.getAuthorities()) {
              authorities.add(new SimpleGrantedAuthority(authority.getValue()));
            }
          }
          return authorities;
        }
      
        @Override
        public boolean isAccountNonExpired() {
          return true;
        }
      
        @Override
        public boolean isAccountNonLocked() {
          return true;
        }
      
        @Override
        public boolean isCredentialsNonExpired() {
          return true;
        }
      
        @Override
        public boolean isEnabled() {
          return true;
        }
      }

      感謝閱讀!

      posted @ 2019-08-02 15:20  張占嶺  閱讀(9688)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 99久久国产综合精品女同| 久久综合伊人77777| 欧美黑人又粗又大久久久| 国产精品七七在线播放| 亚洲性日韩精品一区二区| 国产农村妇女毛片精品久久| 国产日韩一区二区在线| 人妻影音先锋啪啪av资源| 久久99国产精品尤物| 日韩成人一区二区三区在线观看| 毛片网站在线观看| 深夜福利成人免费在线观看| 中文字幕在线日韩一区| 国产成人av三级在线观看| 国产一区二区高清不卡| 崇州市| 亚洲国产婷婷综合在线精品| 久久一区二区中文字幕| 五十路丰满中年熟女中出| 91老肥熟女九色老女人| 久久精品国产中文字幕| 日韩国产成人精品视频| 欧洲码亚洲码的区别入口| 国产午夜精品久久久久免费视 | 亚洲全网成人资源在线观看| 一区二区三区四区自拍视频| 色综合久久久久综合体桃花网| 国产对白老熟女正在播放| 日韩亚av无码一区二区三区 | 亚洲午夜福利精品无码不卡| 亚洲日产韩国一二三四区| 亚洲国家av一区二区| 成人午夜电影福利免费| 高清自拍亚洲精品二区| 久久精品激情亚洲一二区| 久久一本人碰碰人碰| 久久天天躁夜夜躁狠狠85| 日韩人妻精品中文字幕专区| 亚洲鸥美日韩精品久久| 国产高清小视频一区二区| 一区二区三区激情都市|