Skip to content

Commit e71d29c

Browse files
author
nigel.zheng
committed
feat: @ResourcesNonStateless, import a ResourceServerConfigurerAdapter test configuration, which set oauth2 resources to non stateless(token-based)
1 parent 87885a9 commit e71d29c

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ _Note: Most code came from the open network. I refactor and enhanced the code, t
2323
- add `bearer` token to request header to extract a `PreAuthenticatedAuthenticationToken`,
2424
load existing OAuth2Authentication from SecurityContext
2525
- require `@MockTokenServices` on test class
26+
- `@ResourcesNonStateless`
27+
- allow non token-based authentication to access oauth2 resources
2628

2729
## How to use
2830

@@ -89,10 +91,8 @@ or
8991
- [Spring MVC Test Integration](https://docs.spring.io/spring-security/site/docs/current/reference/html/test-mockmvc.html)
9092
- [OAuth2 Autoconfig](https://docs.spring.io/spring-security-oauth2-boot/docs/current/reference/htmlsingle/)
9193
- [Retrieve User Information in Spring Security](https://www.baeldung.com/get-user-in-spring-security)
94+
- [Spring Security OAuth](https://projects.spring.io/spring-security-oauth/docs/Home.html)
9295

9396
## TODOs
9497

95-
- For oauth2 request, add ability to set ResourceServerSecurityConfigurer.stateless to false, maybe add an
96-
annotation like `@ResourceStateless(false)`
97-
9898
- Add support for `RestTemplate`

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ apply plugin: 'java'
1717
apply plugin: "jacoco"
1818

1919
group 'com.github.ahunigel'
20-
version '1.3-SNAPSHOT'
20+
version '1.4-SNAPSHOT'
2121

2222
sourceCompatibility = 1.8
2323

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.github.ahunigel.test.security.oauth2;
2+
3+
import org.springframework.boot.test.context.TestConfiguration;
4+
import org.springframework.context.annotation.Import;
5+
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
6+
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
7+
8+
import java.lang.annotation.ElementType;
9+
import java.lang.annotation.Retention;
10+
import java.lang.annotation.RetentionPolicy;
11+
import java.lang.annotation.Target;
12+
13+
/**
14+
* Created by Nigel Zheng on 8/8/2018.
15+
* <p>
16+
* With this annotation, non-token-based authentication is allowed on these resources.
17+
* Then an incoming cookie can populate the security context and
18+
* allow access to a caller that isn't an OAuth2 client.
19+
*
20+
* @author nigel
21+
*/
22+
@Import({ResourcesNonStateless.NonStatelessResourceServerConfig.class})
23+
@Target(ElementType.TYPE)
24+
@Retention(RetentionPolicy.RUNTIME)
25+
public @interface ResourcesNonStateless {
26+
@TestConfiguration
27+
class NonStatelessResourceServerConfig extends ResourceServerConfigurerAdapter {
28+
29+
@Override
30+
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
31+
resources.stateless(false);
32+
}
33+
}
34+
}

src/main/java/com/github/ahunigel/test/security/oauth2/WithToken.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.lang.annotation.RetentionPolicy;
88

99
/**
10-
* Created by Nigel Zheng on 2018/8/7.
10+
* Created by Nigel Zheng on 8/7/2018.
1111
* <p>
1212
* Emulate an OAuth2 token request, would extract an {@link PreAuthenticatedAuthenticationToken}
1313
* <p>

src/main/java/com/github/ahunigel/test/security/oauth2/WithTokenTestExecutionListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.testSecurityContext;
3030

3131
/**
32-
* Created by Nigel Zheng on 2018/8/6.
32+
* Created by Nigel Zheng on 8/7/2018.
3333
* <p>
3434
* Add <code>Authorization</code> header to token request, extract an {@link PreAuthenticatedAuthenticationToken},
3535
* and then load an existing {@link OAuth2Authentication} from {@link SecurityContext}

0 commit comments

Comments
 (0)