diff --git a/user/build.gradle b/user/build.gradle index 0f7a617..345bf54 100644 --- a/user/build.gradle +++ b/user/build.gradle @@ -28,6 +28,7 @@ ext { dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-security' + implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' compileOnly 'org.projectlombok:lombok' diff --git a/user/src/main/java/com/weather/config/SecurityConfig.java b/user/src/main/java/com/weather/config/SecurityConfig.java new file mode 100644 index 0000000..2e05cbe --- /dev/null +++ b/user/src/main/java/com/weather/config/SecurityConfig.java @@ -0,0 +1,40 @@ +package com.weather.config; + +import lombok.extern.log4j.Log4j2; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; +import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.security.web.SecurityFilterChain; +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; + +@EnableWebSecurity +@Configuration +@Log4j2 +@EnableMethodSecurity(prePostEnabled = true) +public class SecurityConfig { + @Bean + PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } + + @Bean + public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { + AuthenticationManagerBuilder authenticationManagerBuilder = http.getSharedObject(AuthenticationManagerBuilder.class); + AuthenticationManager authenticationManager = authenticationManagerBuilder.build(); + http.authenticationManager(authenticationManager); + + + return http.build(); + } + + @Bean + AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) + throws Exception { + return authenticationConfiguration.getAuthenticationManager(); + } +} diff --git a/user/src/main/java/com/weather/controller/WeatherController.java b/user/src/main/java/com/weather/controller/WeatherController.java index f9a997e..0f9b3f6 100644 --- a/user/src/main/java/com/weather/controller/WeatherController.java +++ b/user/src/main/java/com/weather/controller/WeatherController.java @@ -1,14 +1,20 @@ package com.weather.controller; +import lombok.extern.log4j.Log4j2; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; -@Controller +@RestController +@Log4j2 public class WeatherController { - @PreAuthorize("permitAll()") - @GetMapping("/test") - public String test() { - return "test!!"; + + @GetMapping("/hello") + public String test(Model model) { + log.info("test!!"); + return "test"; } } diff --git a/user/src/main/resources/templates/test.html b/user/src/main/resources/templates/test.html new file mode 100644 index 0000000..e8a1e3d --- /dev/null +++ b/user/src/main/resources/templates/test.html @@ -0,0 +1,10 @@ + + +
+ +