Skip to content

Commit

Permalink
Add @configuration to all web-security config classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ch4mpy committed Dec 26, 2022
1 parent fdf0985 commit 3ee1181
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ all that from properties only
## Web-security config
`spring-oauth2-addons` comes with `@AutoConfiguration` for web-security config adapted to REST API projects. Just add
```java
@EnableMethodSecurity(prePostEnabled = true)
@Configuration
@EnableMethodSecurity
public static class SecurityConfig {
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;

@SpringBootApplication
public class ResourceServerWithOAuthenticationApplication {

public static void main(String[] args) {
SpringApplication.run(ResourceServerWithOAuthenticationApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(ResourceServerWithOAuthenticationApplication.class, args);
}

@EnableMethodSecurity(prePostEnabled = true)
public static class WebSecurityConfig {
}
@Configuration
@EnableMethodSecurity
public static class WebSecurityConfig {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ all that from properties only
By replacing `spring-boot-starter-oauth2-resource-server` with `com.c4-soft.springaddons`:`spring-addons-webmvc-jwt-resource-server:6.0.8`, we can greatly simply web-security configuration:
```java
@EnableMethodSecurity(prePostEnabled = true)
@Configuration
@EnableMethodSecurity
public static class WebSecurityConfig {
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ all that from properties only

## Web-security config
`spring-oauth2-addons` comes with `@AutoConfiguration` for web-security config adapted to REST API projects. We'll just add:
- `@EnableMethodSecurity(prePostEnabled = true)` to activate `@PreAuthorize` on components methods.
- `@EnableMethodSecurity` to activate `@PreAuthorize` on components methods.
- provide an `OAuth2AuthenticationFactory` bean to switch `Authentication` implementation from `JwtAuthenticationToken` to `OAuthentication<OpenidClaimSet>`
```java
@Configuration
@EnableMethodSecurity
public static class SecurityConfig {
@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.core.GrantedAuthority;
Expand All @@ -21,6 +22,7 @@ public static void main(String[] args) {
SpringApplication.run(ResourceServerWithOAuthenticationApplication.class, args);
}

@Configuration
@EnableMethodSecurity
public static class SecurityConfig {
@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ We'll also extend security SpEL with a few methods to:
- evaluate if current user is granted with one of "nice" authorities

```java
@Configuration
@EnableMethodSecurity
public class WebSecurityConfig {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import com.c4_soft.springaddons.security.oauth2.spring.C4MethodSecurityExpressionHandler;
import com.c4_soft.springaddons.security.oauth2.spring.C4MethodSecurityExpressionRoot;

@EnableMethodSecurity(prePostEnabled = true)
@Configuration
@EnableMethodSecurity
public class SecurityConfig {

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import com.c4_soft.springaddons.security.oauth2.config.synchronised.ExpressionInterceptUrlRegistryPostProcessor;

@EnableMethodSecurity(prePostEnabled = true)
@Configuration
@EnableMethodSecurity
public class SecurityConfig {
@Bean
ExpressionInterceptUrlRegistryPostProcessor expressionInterceptUrlRegistryPostProcessor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import com.c4_soft.springaddons.security.oauth2.config.synchronised.ExpressionInterceptUrlRegistryPostProcessor;
import com.c4_soft.springaddons.security.oauth2.config.synchronised.OAuth2AuthenticationFactory;

@EnableMethodSecurity(prePostEnabled = true)
@Configuration
@EnableMethodSecurity
public class SecurityConfig {
@Bean
OAuth2AuthenticationFactory authenticationFactory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import com.c4_soft.springaddons.security.oauth2.config.OAuth2AuthoritiesConverter;
import com.c4_soft.springaddons.security.oauth2.config.synchronised.ExpressionInterceptUrlRegistryPostProcessor;

@EnableMethodSecurity(prePostEnabled = true)
@Configuration
@EnableMethodSecurity
public class SecurityConfig {
@Bean
public ExpressionInterceptUrlRegistryPostProcessor expressionInterceptUrlRegistryPostProcessor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,54 +40,57 @@
@AutoConfigureAddonsSecurity
class MessageServiceTests {

@Autowired
private MessageService messageService;
@Autowired
private MessageService messageService;

@Test()
void greetWitoutAuthentication() {
assertThrows(Exception.class, () -> messageService.getSecret());
}
@Test()
void greetWitoutAuthentication() {
assertThrows(Exception.class, () -> messageService.getSecret());
}

/*--------------*/
/* @WithMockJwt */
/*--------------*/
@Test
@WithMockJwtAuth(authorities = "ROLE_AUTHORIZED_PERSONNEL", claims = @OpenIdClaims(preferredUsername = "ch4mpy"))
void greetWithMockJwtAuth() {
final JwtAuthenticationToken auth = (JwtAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
/*--------------*/
/* @WithMockJwt */
/*--------------*/
@Test
@WithMockJwtAuth(authorities = "ROLE_AUTHORIZED_PERSONNEL", claims = @OpenIdClaims(preferredUsername = "ch4mpy"))
void greetWithMockJwtAuth() {
final JwtAuthenticationToken auth = (JwtAuthenticationToken) SecurityContextHolder.getContext()
.getAuthentication();

assertThat(messageService.greet(auth)).isEqualTo("Hello ch4mpy! You are granted with [ROLE_AUTHORIZED_PERSONNEL].");
}
assertThat(messageService.greet(auth))
.isEqualTo("Hello ch4mpy! You are granted with [ROLE_AUTHORIZED_PERSONNEL].");
}

@Test()
@WithMockJwtAuth()
void secretWithoutAuthorizedPersonnelGrant() {
assertThrows(Exception.class, () -> messageService.getSecret());
}
@Test()
@WithMockJwtAuth()
void secretWithoutAuthorizedPersonnelGrant() {
assertThrows(Exception.class, () -> messageService.getSecret());
}

@Test
@WithMockJwtAuth("ROLE_AUTHORIZED_PERSONNEL")
void secretWithAuthorizedPersonnelRole() {
assertThat(messageService.getSecret()).isEqualTo("Secret message");
}
@Test
@WithMockJwtAuth("ROLE_AUTHORIZED_PERSONNEL")
void secretWithAuthorizedPersonnelRole() {
assertThat(messageService.getSecret()).isEqualTo("Secret message");
}

/*-------------------------*/
/* @WithMockAuthentication */
/*-------------------------*/
@Test
@WithMockAuthentication(authType = JwtAuthenticationToken.class, principalType = Jwt.class, name = "ch4mpy", authorities = "ROLE_AUTHORIZED_PERSONNEL")
void greetWithMockAuthentication() {
final var token = mock(Jwt.class);
when(token.getClaimAsString(StandardClaimNames.PREFERRED_USERNAME)).thenReturn("ch4mpy");
final var auth = (JwtAuthenticationToken) TestSecurityContextHolder.getContext().getAuthentication();
when(auth.getToken()).thenReturn(token);
/*-------------------------*/
/* @WithMockAuthentication */
/*-------------------------*/
@Test
@WithMockAuthentication(authType = JwtAuthenticationToken.class, principalType = Jwt.class, name = "ch4mpy", authorities = "ROLE_AUTHORIZED_PERSONNEL")
void greetWithMockAuthentication() {
final var token = mock(Jwt.class);
when(token.getClaimAsString(StandardClaimNames.PREFERRED_USERNAME)).thenReturn("ch4mpy");
final var auth = (JwtAuthenticationToken) TestSecurityContextHolder.getContext().getAuthentication();
when(auth.getToken()).thenReturn(token);

assertThat(messageService.greet(auth)).isEqualTo("Hello ch4mpy! You are granted with [ROLE_AUTHORIZED_PERSONNEL].");
}
assertThat(messageService.greet(auth))
.isEqualTo("Hello ch4mpy! You are granted with [ROLE_AUTHORIZED_PERSONNEL].");
}

@TestConfiguration(proxyBeanMethods = false)
@EnableMethodSecurity(prePostEnabled = true)
@Import({ MessageService.class })
static class TestConfig {
}
@TestConfiguration(proxyBeanMethods = false)
@EnableMethodSecurity
@Import({ MessageService.class })
static class TestConfig {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import com.c4_soft.springaddons.security.oauth2.config.synchronised.ExpressionInterceptUrlRegistryPostProcessor;

@EnableMethodSecurity(prePostEnabled = true)
@Configuration
@EnableMethodSecurity
public class SecurityConfig {
@Bean
ExpressionInterceptUrlRegistryPostProcessor expressionInterceptUrlRegistryPostProcessor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import com.c4_soft.springaddons.security.oauth2.config.synchronised.ExpressionInterceptUrlRegistryPostProcessor;
import com.c4_soft.springaddons.security.oauth2.config.synchronised.OAuth2AuthenticationFactory;

@EnableMethodSecurity(prePostEnabled = true)
@Configuration
@EnableMethodSecurity
public class SecurityConfig {
@Bean
OAuth2AuthenticationFactory authenticationFactory(
Expand Down
3 changes: 2 additions & 1 deletion webmvc/spring-addons-webmvc-jwt-resource-server/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public class SampleApi {
new SpringApplicationBuilder(SampleApi.class).web(WebApplicationType.SERVLET).run(args);
}

@EnableMethodSecurity(prePostEnabled = true)
@Configuration
@EnableMethodSecurity
public static class WebSecurityConfig {
// browse com.c4_soft.springaddons.security.oauth2.config.synchronised.AddonsSecurityBeans
// for auto-configuration you can override here
Expand Down

0 comments on commit 3ee1181

Please sign in to comment.