參考地址:https://www.jianshu.com/p/19059060036b
session和token的區別:
- session是空間換時間,而token是時間換空間。session占用空間,但是可以管理過期時間,token管理部了過期時間,但是不占用空間.
- sessionId失效問題和token內包含。
- session基於cookie,app請求並沒有cookie 。
- token更加安全(每次請求都需要帶上)
Oauth2 密碼授權流程
在oauth2協議里,每一個應用都有自己的一個clientId和clientSecret(需要去認證方申請),所以一旦想通過認證,必須要有認證方下發的clientId和secret。

1. pom
<!--security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
</dependency>
2. UserDetail實現認證第一步
MyUserDetailsService.java
@Autowired private PasswordEncoder passwordEncoder; /** * 根據進行登錄 * @param username * @return * @throws UsernameNotFoundException */ @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { log.info("登錄用戶名:"+username); String password = passwordEncoder.encode("123456"); //User三個參數 (用戶名+密碼+權限) //根據查找到的用戶信息判斷用戶是否被凍結 log.info("數據庫密碼:"+password); return new User(username,password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin")); }
3. 獲取token的控制器
1 @RestController 2 public class OauthController { 3 4 @Autowired 5 private ClientDetailsService clientDetailsService; 6 @Autowired 7 private AuthorizationServerTokenServices authorizationServerTokenServices; 8 @Autowired 9 private AuthenticationManager authenticationManager; 10 11 @PostMapping("/oauth/getToken") 12 public Object getToken(@RequestParam String username, @RequestParam String password, HttpServletRequest request) throws IOException { 13 Map<String,Object>map = new HashMap<>(8); 14 //進行驗證 15 String header = request.getHeader("Authorization"); 16 if (header == null && !header.startsWith("Basic")) { 17 map.put("code",500); 18 map.put("message","請求投中無client信息"); 19 return map; 20 } 21 String[] tokens = this.extractAndDecodeHeader(header, request); 22 assert tokens.length == 2; 23 //獲取clientId 和 clientSecret 24 String clientId = tokens[0]; 25 String clientSecret = tokens[1]; 26 //獲取 ClientDetails 27 ClientDetails clientDetails = clientDetailsService.loadClientByClientId(clientId); 28 if (clientDetails == null){ 29 map.put("code",500); 30 map.put("message","clientId 不存在"+clientId); 31 return map; 32 //判斷 方言 是否一致 33 }else if (!StringUtils.equals(clientDetails.getClientSecret(),clientSecret)){ 34 map.put("code",500); 35 map.put("message","clientSecret 不匹配"+clientId); 36 return map; 37 } 38 //使用username、密碼進行登錄 39 UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(username, password); 40 //調用指定的UserDetailsService,進行用戶名密碼驗證 41 Authentication authenticate = authenticationManager.authenticate(authentication); 42 HrUtils.setCurrentUser(authenticate); 43 //放到session中 44 //密碼授權 模式, 組建 authentication 45 TokenRequest tokenRequest = new TokenRequest(new HashMap<>(),clientId,clientDetails.getScope(),"password"); 46 47 OAuth2Request oAuth2Request = tokenRequest.createOAuth2Request(clientDetails); 48 OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(oAuth2Request,authentication); 49 50 OAuth2AccessToken token = authorizationServerTokenServices.createAccessToken(oAuth2Authentication); 51 map.put("code",200); 52 map.put("token",token.getValue()); 53 map.put("refreshToken",token.getRefreshToken()); 54 return map; 55 } 56 57 /** 58 * 解碼請求頭 59 */ 60 private String[] extractAndDecodeHeader(String header, HttpServletRequest request) throws IOException { 61 byte[] base64Token = header.substring(6).getBytes("UTF-8"); 62 63 byte[] decoded; 64 try { 65 decoded = Base64.decode(base64Token); 66 } catch (IllegalArgumentException var7) { 67 throw new BadCredentialsException("Failed to decode basic authentication token"); 68 } 69 70 String token = new String(decoded, "UTF-8"); 71 int delim = token.indexOf(":"); 72 if (delim == -1) { 73 throw new BadCredentialsException("Invalid basic authentication token"); 74 } else { 75 return new String[]{token.substring(0, delim), token.substring(delim + 1)}; 76 } 77 } 78 }
4. 核心配置
(1)、Security 配置類 說明登錄方式、登錄頁面、哪個url需要認證、注入登錄失敗/成功過濾器
1 @Configuration 2 public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter { 3 4 /** 5 * 注入 自定義的 登錄成功處理類 6 */ 7 @Autowired 8 private MyAuthenticationSuccessHandler mySuccessHandler; 9 /** 10 * 注入 自定義的 登錄失敗處理類 11 */ 12 @Autowired 13 private MyAuthenticationFailHandler myFailHandler; 14 15 @Autowired 16 private ValidateCodeFilter validateCodeFilter; 17 18 /** 19 * 重寫PasswordEncoder 接口中的方法,實例化加密策略 20 * @return 返回 BCrypt 加密策略 21 */ 22 @Bean 23 public PasswordEncoder passwordEncoder(){ 24 return new BCryptPasswordEncoder(); 25 } 26 27 @Override 28 protected void configure(HttpSecurity http) throws Exception { 29 //在UsernamePasswordAuthenticationFilter 過濾器前 加一個過濾器 來搞驗證碼 30 http.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class) 31 //表單登錄 方式 32 .formLogin() 33 .loginPage("/authentication/require") 34 //登錄需要經過的url請求 35 .loginProcessingUrl("/authentication/form") 36 .passwordParameter("pwd") 37 .usernameParameter("user") 38 .successHandler(mySuccessHandler) 39 .failureHandler(myFailHandler) 40 .and() 41 //請求授權 42 .authorizeRequests() 43 //不需要權限認證的url 44 .antMatchers("/oauth/*","/authentication/*","/code/image").permitAll() 45 //任何請求 46 .anyRequest() 47 //需要身份認證 48 .authenticated() 49 .and() 50 //關閉跨站請求防護 51 .csrf().disable(); 52 //默認注銷地址:/logout 53 http.logout(). 54 //注銷之后 跳轉的頁面 55 logoutSuccessUrl("/authentication/require"); 56 } 57 58 /** 59 * 認證管理 60 * 61 * @return 認證管理對象 62 * @throws Exception 認證異常信息 63 */ 64 @Override 65 @Bean 66 public AuthenticationManager authenticationManagerBean() throws Exception { 67 return super.authenticationManagerBean(); 68 } 69 }
(2)、認證服務器
1 @Configuration 2 @EnableAuthorizationServer 3 public class MyAuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { 4 @Autowired 5 private AuthenticationManager authenticationManager; 6 7 @Autowired 8 private MyUserDetailsService userDetailsService; 9 10 11 12 13 @Override 14 public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { 15 super.configure(security); 16 } 17 18 /** 19 * 客戶端配置(給誰發令牌) 20 * @param clients 21 * @throws Exception 22 */ 23 @Override 24 public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 25 clients.inMemory().withClient("internet_plus") 26 .secret("internet_plus") 27 //有效時間 2小時 28 .accessTokenValiditySeconds(72000) 29 //密碼授權模式和刷新令牌 30 .authorizedGrantTypes("refresh_token","password") 31 .scopes( "all"); 32 } 33 34 @Override 35 public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { 36 endpoints 37 .authenticationManager(authenticationManager) 38 .userDetailsService(userDetailsService); 39 } 40 }
@EnableResourceServer這個注解就決定了這是個資源服務器。它決定了哪些資源需要什么樣的權限。
5、測試

