151. Test login - Part 4 - externalize enabled CORS origins (#18)

This commit is contained in:
Art
2021-09-19 22:41:21 +03:00
parent 18cb20083a
commit 53acf81262
4 changed files with 18 additions and 4 deletions

View File

@ -1,6 +1,7 @@
package net.shyshkin.study.fullstack.supportportal.backend.config; package net.shyshkin.study.fullstack.supportportal.backend.config;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import net.shyshkin.study.fullstack.supportportal.backend.constant.SecurityConstants;
import net.shyshkin.study.fullstack.supportportal.backend.filter.JwtAccessDeniedHandler; import net.shyshkin.study.fullstack.supportportal.backend.filter.JwtAccessDeniedHandler;
import net.shyshkin.study.fullstack.supportportal.backend.filter.JwtAuthenticationEntryPoint; import net.shyshkin.study.fullstack.supportportal.backend.filter.JwtAuthenticationEntryPoint;
import net.shyshkin.study.fullstack.supportportal.backend.filter.JwtAuthorizationFilter; import net.shyshkin.study.fullstack.supportportal.backend.filter.JwtAuthorizationFilter;
@ -16,6 +17,8 @@ import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@EnableWebSecurity @EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) @EnableGlobalMethodSecurity(prePostEnabled = true)
@ -64,4 +67,17 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
return super.authenticationManagerBean(); return super.authenticationManagerBean();
} }
@Bean
public WebMvcConfigurer corsConfigurer(@Value("${app.cors.allowed-origins}") String[] allowedOrigins) {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/user/login")
.allowedOrigins(allowedOrigins)
.exposedHeaders(SecurityConstants.JWT_TOKEN_HEADER);
registry.addMapping("/**").allowedOrigins(allowedOrigins);
}
};
}
} }

View File

@ -2,7 +2,6 @@ package net.shyshkin.study.fullstack.supportportal.backend.controller;
import net.shyshkin.study.fullstack.supportportal.backend.domain.HttpResponse; import net.shyshkin.study.fullstack.supportportal.backend.domain.HttpResponse;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -10,7 +9,6 @@ import static net.shyshkin.study.fullstack.supportportal.backend.utility.HttpRes
import static org.springframework.http.HttpStatus.NOT_FOUND; import static org.springframework.http.HttpStatus.NOT_FOUND;
@RestController @RestController
@CrossOrigin("http://localhost:4200")
public class ErrorController { public class ErrorController {
@GetMapping("/error") @GetMapping("/error")

View File

@ -30,7 +30,6 @@ import static org.springframework.http.HttpStatus.OK;
@RestController @RestController
@RequestMapping("user") @RequestMapping("user")
@RequiredArgsConstructor @RequiredArgsConstructor
@CrossOrigin("http://localhost:4200")
public class UserResource { public class UserResource {
private final UserService userService; private final UserService userService;
@ -48,7 +47,6 @@ public class UserResource {
} }
@PostMapping("login") @PostMapping("login")
@CrossOrigin(value = "http://localhost:4200", exposedHeaders = {SecurityConstants.JWT_TOKEN_HEADER})
public ResponseEntity<User> login(@RequestBody User user) { public ResponseEntity<User> login(@RequestBody User user) {
authenticate(user.getUsername(), user.getPassword()); authenticate(user.getUsername(), user.getPassword());

View File

@ -36,6 +36,8 @@ spring:
# add-mappings: false # add-mappings: false
app: app:
public-urls: /user/login,/user/register,/user/*/image/**,/user/image/** public-urls: /user/login,/user/register,/user/*/image/**,/user/image/**
cors:
allowed-origins: http://localhost:4200,https://localhost:4200
jwt: jwt:
secret: VeRy_5ecretP@55W0rd! secret: VeRy_5ecretP@55W0rd!
# secret: ${random.value} #Does not work - every time generates new value # secret: ${random.value} #Does not work - every time generates new value