|
1 | 1 | package io.javatab.springcloud.eurekaserver.configuration; |
2 | 2 |
|
| 3 | +import org.springframework.beans.factory.annotation.Autowired; |
3 | 4 | import org.springframework.beans.factory.annotation.Value; |
| 5 | +import org.springframework.context.annotation.Bean; |
4 | 6 | import org.springframework.context.annotation.Configuration; |
5 | | -import org.springframework.security.config.Customizer; |
6 | | -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
7 | 7 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
8 | 8 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
9 | | -import org.springframework.security.crypto.password.NoOpPasswordEncoder; |
| 9 | +import org.springframework.security.core.userdetails.User; |
| 10 | +import org.springframework.security.core.userdetails.UserDetails; |
| 11 | +import org.springframework.security.provisioning.InMemoryUserDetailsManager; |
| 12 | +import org.springframework.security.web.SecurityFilterChain; |
10 | 13 |
|
11 | 14 | @Configuration |
12 | 15 | @EnableWebSecurity |
13 | 16 | public class SecurityConfig { |
14 | 17 |
|
15 | | - @Value("${app.eureka-username}") |
16 | | - private String username; |
| 18 | + private final String username; |
| 19 | + private final String password; |
17 | 20 |
|
18 | | - @Value("${app.eureka-password}") |
19 | | - private String password; |
| 21 | + @Autowired |
| 22 | + public SecurityConfig( |
| 23 | + @Value("${app.eureka-username}") String username, |
| 24 | + @Value("${app.eureka-password}") String password |
| 25 | + ) { |
| 26 | + this.username = username; |
| 27 | + this.password = password; |
| 28 | + } |
20 | 29 |
|
21 | | - public void configure(AuthenticationManagerBuilder auth) throws Exception { |
22 | | - auth.inMemoryAuthentication() |
23 | | - .passwordEncoder(NoOpPasswordEncoder.getInstance()) |
24 | | - .withUser(username).password(password) |
25 | | - .authorities("USER"); |
| 30 | + @Bean |
| 31 | + public InMemoryUserDetailsManager userDetailsService() { |
| 32 | + UserDetails user = User.withDefaultPasswordEncoder() |
| 33 | + .username(username) |
| 34 | + .password(password) |
| 35 | + .roles("USER") |
| 36 | + .build(); |
| 37 | + return new InMemoryUserDetailsManager(user); |
26 | 38 | } |
27 | 39 |
|
28 | | - protected void configure(HttpSecurity http) throws Exception { |
| 40 | + @Bean |
| 41 | + public SecurityFilterChain configure(HttpSecurity http) throws Exception { |
29 | 42 | http |
30 | | - // Disable CSRF to allow services to register themselves with Eureka |
| 43 | + // Disable CRCF to allow services to register themselves with Eureka |
31 | 44 | .csrf() |
32 | 45 | .disable() |
33 | | - .authorizeHttpRequests(authorizationManagerRequestMatcherRegistry -> { |
34 | | - try { |
35 | | - authorizationManagerRequestMatcherRegistry |
36 | | - .anyRequest().authenticated().and().httpBasic(Customizer.withDefaults()); |
37 | | - } catch (Exception e) { |
38 | | - e.printStackTrace(); |
39 | | - } |
40 | | - }); |
| 46 | + .authorizeRequests() |
| 47 | + .anyRequest().authenticated() |
| 48 | + .and() |
| 49 | + .httpBasic(); |
| 50 | + return http.build(); |
41 | 51 | } |
42 | 52 | } |
0 commit comments