diff --git a/lab-68/lab-68-demo21-authorization-server-on-sso/pom.xml b/lab-68/lab-68-demo21-authorization-server-on-sso/pom.xml new file mode 100644 index 00000000..bea822c2 --- /dev/null +++ b/lab-68/lab-68-demo21-authorization-server-on-sso/pom.xml @@ -0,0 +1,55 @@ + + + + lab-68 + cn.iocoder.springboot.labs + 1.0-SNAPSHOT + + 4.0.0 + + lab-68-demo21-authorization-server-on-sso + + + + 2.2.4.RELEASE + + 1.8 + 1.8 + + + + + + org.springframework.boot + spring-boot-starter-parent + ${spring.boot.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + + + + + + + org.springframework.security.oauth.boot + spring-security-oauth2-autoconfigure + ${spring.boot.version} + + + + diff --git a/lab-68/lab-68-demo21-authorization-server-on-sso/src/main/java/cn/iocoder/springboot/lab68/authorizationserverdemo/AuthorizationServerApplication.java b/lab-68/lab-68-demo21-authorization-server-on-sso/src/main/java/cn/iocoder/springboot/lab68/authorizationserverdemo/AuthorizationServerApplication.java new file mode 100644 index 00000000..d08ae7c8 --- /dev/null +++ b/lab-68/lab-68-demo21-authorization-server-on-sso/src/main/java/cn/iocoder/springboot/lab68/authorizationserverdemo/AuthorizationServerApplication.java @@ -0,0 +1,13 @@ +package cn.iocoder.springboot.lab68.authorizationserverdemo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class AuthorizationServerApplication { + + public static void main(String[] args) { + SpringApplication.run(AuthorizationServerApplication.class, args); + } + +} diff --git a/lab-68/lab-68-demo21-authorization-server-on-sso/src/main/java/cn/iocoder/springboot/lab68/authorizationserverdemo/config/OAuth2AuthorizationServerConfig.java b/lab-68/lab-68-demo21-authorization-server-on-sso/src/main/java/cn/iocoder/springboot/lab68/authorizationserverdemo/config/OAuth2AuthorizationServerConfig.java new file mode 100644 index 00000000..16c1af22 --- /dev/null +++ b/lab-68/lab-68-demo21-authorization-server-on-sso/src/main/java/cn/iocoder/springboot/lab68/authorizationserverdemo/config/OAuth2AuthorizationServerConfig.java @@ -0,0 +1,48 @@ +package cn.iocoder.springboot.lab68.authorizationserverdemo.config; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.authentication.AuthenticationManager; +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; + +/** + * 授权服务器配置 + */ +@Configuration +@EnableAuthorizationServer +public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { + + /** + * 用户认证 Manager + */ + @Autowired + private AuthenticationManager authenticationManager; + + @Override + public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { + endpoints.authenticationManager(authenticationManager); + } + + @Override + public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception { + oauthServer.checkTokenAccess("isAuthenticated()") +// .tokenKeyAccess("permitAll()") + ; + } + + @Override + public void configure(ClientDetailsServiceConfigurer clients) throws Exception { + clients.inMemory() + .withClient("clientapp").secret("112233") // Client 账号、密码。 + .authorizedGrantTypes("authorization_code") // 授权码模式 + .redirectUris("http://127.0.0.1:9090/login") // 配置回调地址,选填。 + .scopes("read_userinfo", "read_contacts") // 可授权的 Scope +// .and().withClient() // 可以继续配置新的 Client + ; + } + +} diff --git a/lab-68/lab-68-demo21-authorization-server-on-sso/src/main/java/cn/iocoder/springboot/lab68/authorizationserverdemo/config/SecurityConfig.java b/lab-68/lab-68-demo21-authorization-server-on-sso/src/main/java/cn/iocoder/springboot/lab68/authorizationserverdemo/config/SecurityConfig.java new file mode 100644 index 00000000..8f02677b --- /dev/null +++ b/lab-68/lab-68-demo21-authorization-server-on-sso/src/main/java/cn/iocoder/springboot/lab68/authorizationserverdemo/config/SecurityConfig.java @@ -0,0 +1,49 @@ +package cn.iocoder.springboot.lab68.authorizationserverdemo.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.config.BeanIds; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.crypto.password.NoOpPasswordEncoder; + +@Configuration +@EnableWebSecurity +public class SecurityConfig extends WebSecurityConfigurerAdapter { + + @Override + @Bean(name = BeanIds.AUTHENTICATION_MANAGER) + public AuthenticationManager authenticationManagerBean() throws Exception { + return super.authenticationManagerBean(); + } + + @Bean + public static NoOpPasswordEncoder passwordEncoder() { + return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance(); + } + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth. + // 使用内存中的 InMemoryUserDetailsManager + inMemoryAuthentication() + // 不使用 PasswordEncoder 密码编码器 + .passwordEncoder(passwordEncoder()) + // 配置 yunai 用户 + .withUser("yunai").password("1024").roles("USER"); + } + +// @Override +// protected void configure(HttpSecurity http) throws Exception { +// http +// .authorizeRequests() +// .antMatchers("/oauth/**").permitAll() // 允许无权限访问 +// .anyRequest().authenticated() +// .and() +// .formLogin().and() +// .httpBasic(); +// } + +} diff --git a/lab-68/lab-68-demo21-resource-server-on-sso/pom.xml b/lab-68/lab-68-demo21-resource-server-on-sso/pom.xml new file mode 100644 index 00000000..603da2cf --- /dev/null +++ b/lab-68/lab-68-demo21-resource-server-on-sso/pom.xml @@ -0,0 +1,62 @@ + + + + lab-68 + cn.iocoder.springboot.labs + 1.0-SNAPSHOT + + 4.0.0 + + lab-68-demo21-resource-server + + + + 2.2.4.RELEASE + + 1.8 + 1.8 + + + + + + org.springframework.boot + spring-boot-starter-parent + ${spring.boot.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + + + + + + + + + + + + + + org.springframework.security.oauth.boot + spring-security-oauth2-autoconfigure + ${spring.boot.version} + + + + diff --git a/lab-68/lab-68-demo21-resource-server-on-sso/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/ResourceServerApplication.java b/lab-68/lab-68-demo21-resource-server-on-sso/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/ResourceServerApplication.java new file mode 100644 index 00000000..630affa7 --- /dev/null +++ b/lab-68/lab-68-demo21-resource-server-on-sso/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/ResourceServerApplication.java @@ -0,0 +1,13 @@ +package cn.iocoder.springboot.lab68.resourceserverdemo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ResourceServerApplication { + + public static void main(String[] args) { + SpringApplication.run(ResourceServerApplication.class, args); + } + +} diff --git a/lab-68/lab-68-demo21-resource-server-on-sso/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/OAuth2ResourceServerConfig.java b/lab-68/lab-68-demo21-resource-server-on-sso/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/OAuth2ResourceServerConfig.java new file mode 100644 index 00000000..3a5b2d6e --- /dev/null +++ b/lab-68/lab-68-demo21-resource-server-on-sso/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/OAuth2ResourceServerConfig.java @@ -0,0 +1,13 @@ +package cn.iocoder.springboot.lab68.resourceserverdemo.config; + +import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso; +import org.springframework.context.annotation.Configuration; + +/** + * 资源服务器配置 + */ +@Configuration +@EnableOAuth2Sso +public class OAuth2ResourceServerConfig { + +} diff --git a/lab-68/lab-68-demo21-resource-server-on-sso/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/controller/UserController.java b/lab-68/lab-68-demo21-resource-server-on-sso/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/controller/UserController.java new file mode 100644 index 00000000..6ad27b04 --- /dev/null +++ b/lab-68/lab-68-demo21-resource-server-on-sso/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/controller/UserController.java @@ -0,0 +1,18 @@ +package cn.iocoder.springboot.lab68.resourceserverdemo.controller; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * 用户 Controller + */ +@RestController +@RequestMapping("/user") +public class UserController { + + @RequestMapping("/info") + public String hello() { + return "world"; + } + +} diff --git a/lab-68/lab-68-demo21-resource-server-on-sso/src/main/resources/application.yml b/lab-68/lab-68-demo21-resource-server-on-sso/src/main/resources/application.yml new file mode 100644 index 00000000..ffd7dfbc --- /dev/null +++ b/lab-68/lab-68-demo21-resource-server-on-sso/src/main/resources/application.yml @@ -0,0 +1,18 @@ +server: + port: 9090 + servlet: + session: + cookie: + name: OAUTH2-CLIENT-SESSIONID + +security: + oauth2: + # OAuth2 Client 配置,对应 OAuth2ClientProperties 类 + client: + client-id: clientapp + client-secret: 112233 + user-authorization-uri: http://127.0.0.1:8080/oauth/authorize # + access-token-uri: http://127.0.0.1:8080/oauth/token + # OAuth2 Resource 配置,对应 ResourceServerProperties 类 + resource: + token-info-uri: http://127.0.0.1:8080/oauth/check_token # 获得 Token 信息的 URL diff --git a/lab-68/pom.xml b/lab-68/pom.xml index fea61295..5fabcbd1 100644 --- a/lab-68/pom.xml +++ b/lab-68/pom.xml @@ -28,6 +28,9 @@ lab-68-demo11-authorization-server-by-jdbc-store lab-68-demo11-authorization-server-by-redis-store lab-68-demo11-authorization-server-by-jwt-store + + lab-68-demo21-authorization-server-on-sso + lab-68-demo21-resource-server-on-sso diff --git a/lab-68/《芋道 Spring Security OAuth2 存储器》.md b/lab-68/《芋道 Spring Security OAuth2 存储器》.md new file mode 100644 index 00000000..738b41eb --- /dev/null +++ b/lab-68/《芋道 Spring Security OAuth2 存储器》.md @@ -0,0 +1 @@ +