Oauth2認證模式 - 授權碼模式(authorization code)
1. 交互流程
交互流程如下
2. 實現步驟
實現 oauth2 需要三個步驟
-
配置security
-
配置授權服務器
-
配置資源服務器
2.1 pom.xml添加maven依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-test</artifactid>
<scope>test</scope>
</dependency>
<dependency>
<groupid>org.springframework.cloud</groupid>
<artifactid>spring-cloud-starter-security</artifactid>
</dependency>
<dependency>
<groupid>org.springframework.cloud</groupid>
<artifactid>spring-cloud-starter-oauth2</artifactid>
</dependency>
</dependencies>
<dependencymanagement>
<dependencies>
<dependency>
<groupid>org.springframework.cloud</groupid>
<artifactid>spring-cloud-dependencies</artifactid>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencymanagement>
2.2 配置Security
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/oauth/**","/login/**","logout/**")
.permitAll()
.anyRequest().authenticated()
.and()
.formLogin().permitAll();
}
}
2.3 配置授權服務器
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private PasswordEncoder passwordEncoder;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("admin")
.secret(passwordEncoder.encode("123"))
// 配置訪問有效期
//.accessTokenValiditySeconds(3600)
// 配置成功后跳轉地址
.redirectUris("http://www.baidu.com")
// 配置授權范圍
.scopes("all")
// 配置grant_type模式為授權碼模式
.authorizedGrantTypes("authorization_code");
}
}
2.4 配置資源服務器
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.requestMatchers().antMatchers("/user/**");
}
}
2.5
實現UserDetailsService,授權賬戶
@Service
public class UserService implements UserDetailsService {
@Autowired
private PasswordEncoder passwordEncoder;
@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
//設置用戶密碼
String password = passwordEncoder.encode("123456");
//創建用戶以及權限
return new User("admin",password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
}
}
3. 開始測試
3.1 瀏覽器訪問(參數對應授權服務器中的配置)
3.2 選擇同意授權
3.3 授權成功后自動跳轉至配置好的跳轉地址,獲取授權碼
3.4 postman中請求獲取token,賬戶和密碼為授權服務器中配置的賬戶密碼
3.5 填寫基本信息后,獲取token認證信息