Skip to content

Commit 2cc341b

Browse files
committed
Don't call bean methods from configs
1 parent f696b6c commit 2cc341b

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

examples/security-grpc-bearerAuth-server/src/main/java/net/devh/boot/grpc/examples/security/server/SecurityConfiguration.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@
5151
public class SecurityConfiguration {
5252

5353
@Bean
54-
JwtAuthenticationConverter jwtAuthenticationConverter() {
54+
JwtAuthenticationConverter jwtAuthenticationConverter(
55+
final KeyCloakGrantedAuthoritiesConverter keyCloakGrantedAuthoritiesConverter) {
56+
5557
final JwtAuthenticationConverter converter = new JwtAuthenticationConverter();
56-
converter.setJwtGrantedAuthoritiesConverter(keyCloakGrantedAuthoritiesConverter());
58+
converter.setJwtGrantedAuthoritiesConverter(keyCloakGrantedAuthoritiesConverter);
5759
return converter;
5860
}
5961

@@ -63,26 +65,26 @@ KeyCloakGrantedAuthoritiesConverter keyCloakGrantedAuthoritiesConverter() {
6365
}
6466

6567
@Bean
66-
JwtAuthenticationProvider jwtAuthenticationProvider() {
68+
JwtAuthenticationProvider jwtAuthenticationProvider(final JwtAuthenticationConverter jwtAuthenticationConverter) {
6769
final JwtAuthenticationProvider provider = new JwtAuthenticationProvider(jwtDecoder());
68-
provider.setJwtAuthenticationConverter(jwtAuthenticationConverter());
70+
provider.setJwtAuthenticationConverter(jwtAuthenticationConverter);
6971
return provider;
7072
}
7173

7274
@Bean
7375
/*
7476
* Add the authentication providers to the manager.
7577
*/
76-
AuthenticationManager authenticationManager() {
78+
AuthenticationManager authenticationManager(final JwtAuthenticationProvider jwtAuthenticationProvider) {
7779
final List<AuthenticationProvider> providers = new ArrayList<>();
78-
providers.add(jwtAuthenticationProvider());
80+
providers.add(jwtAuthenticationProvider);
7981
return new ProviderManager(providers);
8082
}
8183

8284
@Bean
8385
// Configure which authentication types you support.
8486
GrpcAuthenticationReader authenticationReader() {
85-
return new BearerAuthenticationReader(accessToken -> new BearerTokenAuthenticationToken(accessToken));
87+
return new BearerAuthenticationReader(BearerTokenAuthenticationToken::new);
8688
}
8789

8890
@Bean

examples/security-grpc-server/src/main/java/net/devh/boot/grpc/examples/security/server/SecurityConfiguration.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,17 @@ PasswordEncoder passwordEncoder() {
6565

6666
@Bean
6767
// This could be your database lookup. There are some complete implementations in spring-security-web.
68-
UserDetailsService userDetailsService() {
68+
UserDetailsService userDetailsService(final PasswordEncoder passwordEncoder) {
6969
return username -> {
7070
log.debug("Searching user: {}", username);
7171
switch (username) {
7272
case "guest": {
73-
return new User(username, passwordEncoder().encode(username + "Password"), Collections.emptyList());
73+
return new User(username, passwordEncoder.encode(username + "Password"), Collections.emptyList());
7474
}
7575
case "user": {
7676
final List<SimpleGrantedAuthority> authorities =
7777
Arrays.asList(new SimpleGrantedAuthority("ROLE_GREET"));
78-
return new User(username, passwordEncoder().encode(username + "Password"), authorities);
78+
return new User(username, passwordEncoder.encode(username + "Password"), authorities);
7979
}
8080
default: {
8181
throw new UsernameNotFoundException("Could not find user!");
@@ -87,18 +87,21 @@ UserDetailsService userDetailsService() {
8787
@Bean
8888
// One of your authentication providers.
8989
// They ensure that the credentials are valid and populate the user's authorities.
90-
DaoAuthenticationProvider daoAuthenticationProvider() {
90+
DaoAuthenticationProvider daoAuthenticationProvider(
91+
final UserDetailsService userDetailsService,
92+
final PasswordEncoder passwordEncoder) {
93+
9194
final DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
92-
provider.setUserDetailsService(userDetailsService());
93-
provider.setPasswordEncoder(passwordEncoder());
95+
provider.setUserDetailsService(userDetailsService);
96+
provider.setPasswordEncoder(passwordEncoder);
9497
return provider;
9598
}
9699

97100
@Bean
98101
// Add the authentication providers to the manager.
99-
AuthenticationManager authenticationManager() {
102+
AuthenticationManager authenticationManager(final DaoAuthenticationProvider daoAuthenticationProvider) {
100103
final List<AuthenticationProvider> providers = new ArrayList<>();
101-
providers.add(daoAuthenticationProvider());
104+
providers.add(daoAuthenticationProvider);
102105
return new ProviderManager(providers);
103106
}
104107

0 commit comments

Comments
 (0)