forked from hansonwang99/Spring-Boot-In-Action
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
基于Spring Security Oauth2的SSO单点登录+JWT权限控制实践
- Loading branch information
1 parent
a811fe5
commit d3ca41e
Showing
17 changed files
with
450 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>springbt_sso_jwt</artifactId> | ||
<groupId>cn.codesheep</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>codesheep-client1</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-oauth2</artifactId> | ||
<!--<version>2.0.1.RELEASE</version>--> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
12 changes: 12 additions & 0 deletions
12
springbt_sso_jwt/codesheep-client1/src/main/java/cn/codesheep/Client1Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package cn.codesheep; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class Client1Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Client1Application.class, args); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
..._jwt/codesheep-client1/src/main/java/cn/codesheep/config/ClientWebsecurityConfigurer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cn.codesheep.config; | ||
|
||
|
||
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
@EnableGlobalMethodSecurity(prePostEnabled = true) | ||
@EnableOAuth2Sso | ||
public class ClientWebsecurityConfigurer extends WebSecurityConfigurerAdapter { | ||
|
||
@Override | ||
public void configure(HttpSecurity http) throws Exception { | ||
http.antMatcher("/**").authorizeRequests() | ||
.anyRequest().authenticated(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
springbt_sso_jwt/codesheep-client1/src/main/java/cn/codesheep/controller/TestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package cn.codesheep.controller; | ||
|
||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class TestController { | ||
|
||
@GetMapping("/normal") | ||
@PreAuthorize("hasAuthority('ROLE_NORMAL')") | ||
public String normal( ) { | ||
return "normal permission test success !!!"; | ||
} | ||
|
||
@GetMapping("/medium") | ||
@PreAuthorize("hasAuthority('ROLE_MEDIUM')") | ||
public String medium() { | ||
return "medium permission test success !!!"; | ||
} | ||
|
||
@GetMapping("/admin") | ||
@PreAuthorize("hasAuthority('ROLE_ADMIN')") | ||
public String admin() { | ||
return "admin permission test success !!!"; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
springbt_sso_jwt/codesheep-client1/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
auth-server: http://localhost:8085/uac | ||
server: | ||
port: 8086 | ||
|
||
security: | ||
oauth2: | ||
client: | ||
client-id: sheep1 | ||
client-secret: 123456 | ||
user-authorization-uri: ${auth-server}/oauth/authorize | ||
access-token-uri: ${auth-server}/oauth/token | ||
resource: | ||
jwt: | ||
key-uri: ${auth-server}/oauth/token_key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>springbt_sso_jwt</artifactId> | ||
<groupId>cn.codesheep</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>codesheep-client2</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-oauth2</artifactId> | ||
<!--<version>2.0.1.RELEASE</version>--> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
12 changes: 12 additions & 0 deletions
12
springbt_sso_jwt/codesheep-client2/src/main/java/cn/codesheep/Client2Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package cn.codesheep; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class Client2Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Client2Application.class, args); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
..._jwt/codesheep-client2/src/main/java/cn/codesheep/config/ClientWebsecurityConfigurer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cn.codesheep.config; | ||
|
||
|
||
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
@EnableGlobalMethodSecurity(prePostEnabled = true) | ||
@EnableOAuth2Sso | ||
public class ClientWebsecurityConfigurer extends WebSecurityConfigurerAdapter { | ||
|
||
@Override | ||
public void configure(HttpSecurity http) throws Exception { | ||
http.antMatcher("/**").authorizeRequests() | ||
.anyRequest().authenticated(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
springbt_sso_jwt/codesheep-client2/src/main/java/cn/codesheep/controller/TestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package cn.codesheep.controller; | ||
|
||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class TestController { | ||
|
||
@GetMapping("/normal") | ||
@PreAuthorize("hasAuthority('ROLE_NORMAL')") | ||
public String normal( ) { | ||
return "normal permission test success !!!"; | ||
} | ||
|
||
@GetMapping("/medium") | ||
@PreAuthorize("hasAuthority('ROLE_MEDIUM')") | ||
public String medium() { | ||
return "medium permission test success !!!"; | ||
} | ||
|
||
@GetMapping("/admin") | ||
@PreAuthorize("hasAuthority('ROLE_ADMIN')") | ||
public String admin() { | ||
return "admin permission test success !!!"; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
springbt_sso_jwt/codesheep-client2/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
auth-server: http://localhost:8085/uac | ||
server: | ||
port: 8087 | ||
|
||
security: | ||
oauth2: | ||
client: | ||
client-id: sheep2 | ||
client-secret: 123456 | ||
user-authorization-uri: ${auth-server}/oauth/authorize | ||
access-token-uri: ${auth-server}/oauth/token | ||
resource: | ||
jwt: | ||
key-uri: ${auth-server}/oauth/token_key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>springbt_sso_jwt</artifactId> | ||
<groupId>cn.codesheep</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>codesheep-server</artifactId> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>6</source> | ||
<target>6</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-oauth2</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
springbt_sso_jwt/codesheep-server/src/main/java/cn/codesheep/ServerApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cn.codesheep; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | ||
|
||
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class}) | ||
public class ServerApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(ServerApplication.class, args); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...sso_jwt/codesheep-server/src/main/java/cn/codesheep/config/AuthorizationServerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package cn.codesheep.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer; | ||
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; | ||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer; | ||
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer; | ||
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer; | ||
import org.springframework.security.oauth2.provider.token.DefaultTokenServices; | ||
import org.springframework.security.oauth2.provider.token.TokenStore; | ||
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; | ||
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
|
||
@Configuration | ||
@EnableAuthorizationServer | ||
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { | ||
|
||
@Override | ||
public void configure(ClientDetailsServiceConfigurer clients) throws Exception { | ||
|
||
// 定义了两个客户端应用的通行证 | ||
clients.inMemory() | ||
.withClient("sheep1") | ||
.secret(new BCryptPasswordEncoder().encode("123456")) | ||
.authorizedGrantTypes("authorization_code", "refresh_token") | ||
.scopes("all") | ||
.autoApprove(false) | ||
.and() | ||
.withClient("sheep2") | ||
.secret(new BCryptPasswordEncoder().encode("123456")) | ||
.authorizedGrantTypes("authorization_code", "refresh_token") | ||
.scopes("all") | ||
.autoApprove(false); | ||
} | ||
|
||
@Override | ||
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { | ||
|
||
endpoints.tokenStore(jwtTokenStore()).accessTokenConverter(jwtAccessTokenConverter()); | ||
DefaultTokenServices tokenServices = (DefaultTokenServices) endpoints.getDefaultAuthorizationServerTokenServices(); | ||
tokenServices.setTokenStore(endpoints.getTokenStore()); | ||
tokenServices.setSupportRefreshToken(true); | ||
tokenServices.setClientDetailsService(endpoints.getClientDetailsService()); | ||
tokenServices.setTokenEnhancer(endpoints.getTokenEnhancer()); | ||
tokenServices.setAccessTokenValiditySeconds((int) TimeUnit.DAYS.toSeconds(1)); // 一天有效期 | ||
endpoints.tokenServices(tokenServices); | ||
} | ||
|
||
@Override | ||
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { | ||
security.tokenKeyAccess("isAuthenticated()"); | ||
} | ||
|
||
@Bean | ||
public TokenStore jwtTokenStore() { | ||
return new JwtTokenStore(jwtAccessTokenConverter()); | ||
} | ||
|
||
@Bean | ||
public JwtAccessTokenConverter jwtAccessTokenConverter(){ | ||
JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); | ||
converter.setSigningKey("testKey"); | ||
return converter; | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
...ngbt_sso_jwt/codesheep-server/src/main/java/cn/codesheep/config/SpringSecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package cn.codesheep.config; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.dao.DaoAuthenticationProvider; | ||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
||
@Configuration | ||
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { | ||
|
||
@Override | ||
@Bean | ||
public AuthenticationManager authenticationManager() throws Exception { | ||
return super.authenticationManager(); | ||
} | ||
|
||
@Autowired | ||
private UserDetailsService userDetailsService; | ||
|
||
@Bean | ||
public PasswordEncoder passwordEncoder() { | ||
return new BCryptPasswordEncoder(); | ||
} | ||
|
||
@Bean | ||
public DaoAuthenticationProvider authenticationProvider() { | ||
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider(); | ||
authenticationProvider.setUserDetailsService(userDetailsService); | ||
authenticationProvider.setPasswordEncoder(passwordEncoder()); | ||
authenticationProvider.setHideUserNotFoundExceptions(false); | ||
return authenticationProvider; | ||
} | ||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
|
||
http | ||
.requestMatchers().antMatchers("/oauth/**","/login/**","/logout/**") | ||
.and() | ||
.authorizeRequests() | ||
.antMatchers("/oauth/**").authenticated() | ||
.and() | ||
.formLogin().permitAll(); | ||
} | ||
|
||
@Override | ||
protected void configure(AuthenticationManagerBuilder auth) throws Exception { | ||
auth.authenticationProvider(authenticationProvider()); | ||
} | ||
|
||
} | ||
|
Oops, something went wrong.