File tree Expand file tree Collapse file tree
spring-security-sso-auth-server
test/java/org/baeldung/test
test/java/org/baeldung/test Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
2+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
3+ <modelVersion >4.0.0</modelVersion >
4+ <groupId >org.baeldung</groupId >
5+ <artifactId >spring-security-sso</artifactId >
6+ <version >1.0.0-SNAPSHOT</version >
7+
8+ <name >spring-security-sso</name >
9+ <packaging >pom</packaging >
10+
11+ <parent >
12+ <artifactId >parent-boot-5</artifactId >
13+ <groupId >com.baeldung</groupId >
14+ <version >0.0.1-SNAPSHOT</version >
15+ <relativePath >../parent-boot-5</relativePath >
16+ </parent >
17+
18+ <modules >
19+ <module >spring-security-sso-auth-server</module >
20+ <module >spring-security-sso-ui</module >
21+ </modules >
22+
23+ <properties >
24+ <rest-assured .version>3.0.1</rest-assured .version>
25+ </properties >
26+
27+ </project >
Original file line number Diff line number Diff line change 1+ <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" >
2+ <modelVersion >4.0.0</modelVersion >
3+ <artifactId >spring-security-sso-auth-server</artifactId >
4+
5+ <name >spring-security-sso-auth-server</name >
6+ <packaging >war</packaging >
7+
8+ <parent >
9+ <groupId >org.baeldung</groupId >
10+ <artifactId >spring-security-sso</artifactId >
11+ <version >1.0.0-SNAPSHOT</version >
12+ </parent >
13+
14+ <dependencies >
15+
16+ <dependency >
17+ <groupId >org.springframework.boot</groupId >
18+ <artifactId >spring-boot-starter-web</artifactId >
19+ </dependency >
20+
21+ <dependency >
22+ <groupId >org.springframework.security.oauth</groupId >
23+ <artifactId >spring-security-oauth2</artifactId >
24+ </dependency >
25+
26+ </dependencies >
27+
28+ </project >
Original file line number Diff line number Diff line change 1+ package org .baeldung .config ;
2+
3+ import org .springframework .beans .factory .annotation .Autowired ;
4+ import org .springframework .context .annotation .Configuration ;
5+ import org .springframework .security .authentication .AuthenticationManager ;
6+ import org .springframework .security .oauth2 .config .annotation .configurers .ClientDetailsServiceConfigurer ;
7+ import org .springframework .security .oauth2 .config .annotation .web .configuration .AuthorizationServerConfigurerAdapter ;
8+ import org .springframework .security .oauth2 .config .annotation .web .configuration .EnableAuthorizationServer ;
9+ import org .springframework .security .oauth2 .config .annotation .web .configurers .AuthorizationServerEndpointsConfigurer ;
10+ import org .springframework .security .oauth2 .config .annotation .web .configurers .AuthorizationServerSecurityConfigurer ;
11+
12+ @ Configuration
13+ @ EnableAuthorizationServer
14+ public class AuthServerConfig extends AuthorizationServerConfigurerAdapter {
15+ @ Autowired
16+ private AuthenticationManager authenticationManager ;
17+
18+ @ Override
19+ public void configure (final AuthorizationServerSecurityConfigurer oauthServer ) throws Exception {
20+ oauthServer .tokenKeyAccess ("permitAll()" )
21+ .checkTokenAccess ("isAuthenticated()" );
22+ }
23+
24+ @ Override
25+ public void configure (final ClientDetailsServiceConfigurer clients ) throws Exception {
26+ clients .inMemory ()
27+ .withClient ("SampleClientId" )
28+ .secret ("secret" )
29+ .authorizedGrantTypes ("authorization_code" )
30+ .scopes ("user_info" )
31+ .autoApprove (true )
32+ // .accessTokenValiditySeconds(3600)
33+ ; // 1 hour
34+ }
35+
36+ @ Override
37+ public void configure (final AuthorizationServerEndpointsConfigurer endpoints ) throws Exception {
38+ endpoints .authenticationManager (authenticationManager );
39+ }
40+
41+ }
Original file line number Diff line number Diff line change 1+ package org .baeldung .config ;
2+
3+ import org .springframework .boot .SpringApplication ;
4+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
5+ import org .springframework .boot .web .support .SpringBootServletInitializer ;
6+ import org .springframework .security .oauth2 .config .annotation .web .configuration .EnableResourceServer ;
7+
8+ @ SpringBootApplication
9+ @ EnableResourceServer
10+ public class AuthorizationServerApplication extends SpringBootServletInitializer {
11+
12+ public static void main (String [] args ) {
13+ SpringApplication .run (AuthorizationServerApplication .class , args );
14+ }
15+
16+ }
Original file line number Diff line number Diff line change 1+ package org .baeldung .config ;
2+
3+ import org .springframework .beans .factory .annotation .Autowired ;
4+ import org .springframework .context .annotation .Configuration ;
5+ import org .springframework .security .authentication .AuthenticationManager ;
6+ import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
7+ import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
8+ import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
9+
10+ @ Configuration
11+ public class SecurityConfig extends WebSecurityConfigurerAdapter {
12+
13+ @ Autowired
14+ private AuthenticationManager authenticationManager ;
15+
16+ @ Override
17+ protected void configure (HttpSecurity http ) throws Exception {
18+ http .requestMatchers ()
19+ .antMatchers ("/login" , "/oauth/authorize" )
20+ .and ()
21+ .authorizeRequests ()
22+ .anyRequest ()
23+ .authenticated ()
24+ .and ()
25+ .formLogin ()
26+ .permitAll ();
27+ }
28+
29+ @ Override
30+ protected void configure (AuthenticationManagerBuilder auth ) throws Exception {
31+ auth .parentAuthenticationManager (authenticationManager )
32+ .inMemoryAuthentication ()
33+ .withUser ("john" )
34+ .password ("123" )
35+ .roles ("USER" );
36+ }
37+
38+ }
Original file line number Diff line number Diff line change 1+ package org .baeldung .config ;
2+
3+ import java .security .Principal ;
4+
5+ import org .springframework .web .bind .annotation .RequestMapping ;
6+ import org .springframework .web .bind .annotation .RestController ;
7+
8+ @ RestController
9+ public class UserController {
10+
11+ @ RequestMapping ("/user/me" )
12+ public Principal user (Principal principal ) {
13+ System .out .println (principal );
14+ return principal ;
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ server.port =8081
2+ server.context-path =/auth
3+ security.basic.enabled =false
4+ # logging.level.org.springframework=DEBUG
Original file line number Diff line number Diff line change 1+ package org .baeldung .test ;
2+
3+ import org .baeldung .config .AuthorizationServerApplication ;
4+ import org .junit .Test ;
5+ import org .junit .runner .RunWith ;
6+ import org .springframework .boot .test .context .SpringBootTest ;
7+ import org .springframework .boot .test .context .SpringBootTest .WebEnvironment ;
8+ import org .springframework .test .context .junit4 .SpringRunner ;
9+
10+ @ RunWith (SpringRunner .class )
11+ @ SpringBootTest (classes = AuthorizationServerApplication .class , webEnvironment = WebEnvironment .RANDOM_PORT )
12+ public class AuthServerIntegrationTest {
13+
14+ @ Test
15+ public void whenLoadApplication_thenSuccess () {
16+
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ <project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
2+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
3+ <modelVersion >4.0.0</modelVersion >
4+ <artifactId >spring-security-sso-ui</artifactId >
5+
6+ <name >spring-security-sso-ui</name >
7+ <packaging >war</packaging >
8+
9+ <parent >
10+ <groupId >org.baeldung</groupId >
11+ <artifactId >spring-security-sso</artifactId >
12+ <version >1.0.0-SNAPSHOT</version >
13+ </parent >
14+
15+ <dependencies >
16+
17+ <dependency >
18+ <groupId >org.springframework.boot</groupId >
19+ <artifactId >spring-boot-starter-web</artifactId >
20+ </dependency >
21+
22+ <dependency >
23+ <groupId >org.springframework.boot</groupId >
24+ <artifactId >spring-boot-starter-security</artifactId >
25+ </dependency >
26+
27+ <dependency >
28+ <groupId >org.springframework.security.oauth</groupId >
29+ <artifactId >spring-security-oauth2</artifactId >
30+ </dependency >
31+
32+ <dependency >
33+ <groupId >org.springframework.boot</groupId >
34+ <artifactId >spring-boot-starter-thymeleaf</artifactId >
35+ </dependency >
36+
37+ <dependency >
38+ <groupId >org.thymeleaf.extras</groupId >
39+ <artifactId >thymeleaf-extras-springsecurity4</artifactId >
40+ </dependency >
41+
42+ </dependencies >
43+
44+ </project >
Original file line number Diff line number Diff line change 1+ package org .baeldung .config ;
2+
3+ import org .springframework .boot .SpringApplication ;
4+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
5+ import org .springframework .boot .autoconfigure .security .oauth2 .client .EnableOAuth2Sso ;
6+ import org .springframework .context .annotation .Bean ;
7+ import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
8+ import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
9+ import org .springframework .web .context .request .RequestContextListener ;
10+
11+ @ EnableOAuth2Sso
12+ @ SpringBootApplication
13+ public class UiApplication extends WebSecurityConfigurerAdapter {
14+
15+ @ Override
16+ public void configure (HttpSecurity http ) throws Exception {
17+ http .antMatcher ("/**" )
18+ .authorizeRequests ()
19+ .antMatchers ("/" , "/login**" )
20+ .permitAll ()
21+ .anyRequest ()
22+ .authenticated ();
23+ }
24+
25+ @ Bean
26+ public RequestContextListener requestContextListener () {
27+ return new RequestContextListener ();
28+ }
29+
30+ public static void main (String [] args ) {
31+ SpringApplication .run (UiApplication .class , args );
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments