https://github.com/chensoul/spring-authorization-server
a local spring authorization server for test
https://github.com/chensoul/spring-authorization-server
spring-boot spring-security spring-security-oauth2
Last synced: 4 months ago
JSON representation
a local spring authorization server for test
- Host: GitHub
- URL: https://github.com/chensoul/spring-authorization-server
- Owner: chensoul
- License: apache-2.0
- Created: 2024-12-06T01:39:59.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-02-06T00:54:21.000Z (5 months ago)
- Last Synced: 2025-02-06T01:29:18.938Z (5 months ago)
- Topics: spring-boot, spring-security, spring-security-oauth2
- Language: Java
- Homepage:
- Size: 63.5 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# spring-authorization-server
## How to use
Package the application using the maven command:
```bash
./mvnw clean package
```Run the application with `--config` argument for the sample file `samples/config.yml`:
```yaml
application:
spring-authorization-server:
users:
- username: alice
password: alice
attributes:
email: [email protected]
roles:
- viewer
- editor
- admin
- username: bob
password: bob
attributes:
email: [email protected]
roles:
- viewer
- editor
```Then run the application:
```bash
java -jar target/spring-authorization-server-0.0.1-SNAPSHOT.jar --config=samples/config.yml
```## Using in a client application
### Config the client application
Copy the sample configuration that the authorization server prints out in the console, and use it in your client
application.Make sure the `openid` scope is included in the client configuration.Finally, configure your client application to extract authorities from the custom `roles` claim, by providing an `OidcUserService` bean:
```java
@Bean
OidcUserService oidcUserService() {
var oidcUserService = new OidcUserService();
oidcUserService.setOidcUserMapper((oidcUserRequest, oidcUserInfo) -> {
// Will map the "roles" claim from the `id_token` into user authorities (roles)
var roles = oidcUserRequest.getIdToken().getClaimAsStringList("roles");
var authorities = AuthorityUtils.createAuthorityList();
if (roles!=null) {
roles.stream()
.map(r -> "ROLE_" + r)
.map(SimpleGrantedAuthority::new)
.forEach(authorities::add);
}
return new DefaultOidcUser(authorities, oidcUserRequest.getIdToken(), oidcUserInfo);
});
return oidcUserService;
}
```Roles can then be checked in request or method security:
```java
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http
.authorizeHttpRequests(auth -> {
auth.requestMatchers("/public/**").permitAll();
auth.requestMatchers("/document/**").hasAnyRole("viewer", "editor", "admin");
auth.requestMatchers("/admin/**").hasRole("admin");
auth.anyRequest().authenticated();
})
.oauth2Login(Customizer.withDefaults())
.build();
}
```### Using in tests with Testcontainers
First add the `testcontainers` dependency to your project, for example pom.xml in a maven project:
```xml
org.springframework.boot
spring-boot-starter-test
org.springframework.boot
spring-boot-testcontainers
org.testcontainers
junit-jupiter
```Then, configure `@SpringBootTests` to use Spring Authorization Server + Testcontainers:
```java
@Testcontainers(disabledWithoutDocker = true)
@SpringBootTest
class TestcontainersTests {@Container
static GenericContainer> authServer = new GenericContainer<>("chensoul/spring-authorization-server:0.0.1")
.withExposedPorts(9000);@DynamicPropertySource
static void clientRegistrationProperties(DynamicPropertyRegistry registry) {
registry.add("spring.security.oauth2.client.provider.spring-authorization-server.issuer-uri",
() -> "http://localhost:" + authServer.getExposedPorts().get(0));
}@Test
void contextLoads() {
}
}
```## Enable AOT
Install GraalVM JDK:
```bash
sdk install java 21.0.5-graal
```Run maven command to compile the native image:
```bash
./mvnw -Pnative native:compile
```## Run with Docker
Create an image with [buildpack](https://buildpacks.io/).
```bash
brew install buildpacks/tap/packpack build spring-authorization-server:0.0.1 \
--path ./spring-authorization-server-0.0.1-SNAPSHOT.jar \
--builder paketobuildpacks/builder:tiny
```> If you will be running the image on an ARM host (such as an Apple machine with an Apple chipset), you must use a
> different builder:
>
> ```bash
> pack build spring-authorization-server:0.0.1 \
> --path target/spring-authorization-server-0.0.1-SNAPSHOT.jar \
> --builder dashaun/builder:tiny
> ```Or you can create an image using docker build.
```bash
docker build -t chensoul/spring-authorization-server:0.0.1 .
```Start the container by running:
```bash
docker run -d -p 9000:9000 chensoul/spring-authorization-server:0.0.1
```Alternatively, you can push the image to docker hub:
```bash
docker login
docker tag chensoul/spring-authorization-server:0.0.1 chensoul/spring-authorization-server:latest
docker push chensoul/spring-authorization-server:0.0.1
```