# skykingkong-common-oauth2
**Repository Path**: hexagonframework/skykingkong-common-oauth2
## Basic Information
- **Project Name**: skykingkong-common-oauth2
- **Description**: No description available
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 1
- **Created**: 2017-12-20
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
#skykingkong-common-oauth2
> 设置应用成为OAuth2 Client & ResourceServer,使用FeignClient访问HTTP MicroService
* 在项目中引入依赖
```xml
org.springframework.cloud
spring-cloud-starter-oauth2
org.springframework.boot
spring-boot-starter-security
org.springframework.security.oauth
spring-security-oauth2
org.springframework.cloud
spring-cloud-starter-feign
com.jkgj.skykingkong
skykingkong-common-oauth2
1.4
```
* 在Application类上加上配置注解:
```java
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableCustomUserInfoTokenService // 使用自定义UserInfoTokenService获取当前已认证用户
@EnableOAuth2Client
@EnableFeignClients
@EnableCustomOAuth2FeignRequestInterceptor // 使用自定义OAuth2FeignRequestInterceptor自动添加Authorization头
```
* 自定义安全访问配置
```java
@SpringBootApplication
@EnableDiscoveryClient
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableCustomUserInfoTokenService
@EnableOAuth2Client
@EnableFeignClients
@EnableCustomOAuth2FeignRequestInterceptor
@Configuration
public class DemoApplication extends ResourceServerConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void configure(HttpSecurity http) throws Exception {
// 配置授权
http.authorizeRequests()
.antMatchers("/", "/demo").permitAll()
.anyRequest().authenticated();
}
}
```
* 获取登录用户信息
```java
action(@AuthenticationPrincipal UserPrincipal userPrincipal)
```