admin管理员组文章数量:1122847
i have a microservice for logging in using oauth2. it gets a login request from the frontend that is routed through a gateway.
the service works just fine when i run it normally using mvn spring-boot:run
but when running it in a docker container the login page cant be reached. i dont understand why.
the services security config
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(authorize -> authorize
.requestMatchers("/actuator/**", "/api/login/**").permitAll()
.anyRequest().authenticated()
)
.oauth2Login(oauth2 -> oauth2
.loginPage("/oauth2/authorization/google")
.successHandler(authenticationSuccessHandler())
)
.csrf(csrf -> csrf.disable()); // Disable CSRF if needed
return http.build();
}
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(List.of("http://localhost:3000","http://localhost:8000")); // Allow specific origins
configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS")); // Allow specific methods
configuration.setAllowedHeaders(List.of("Authorization", "Content-Type", "Accept")); // Allow specific headers
configuration.setAllowCredentials(true); // Allow credentials like cookies
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration); // Apply CORS settings to all endpoints
return source;
}
@Bean
public SimpleUrlAuthenticationSuccessHandler authenticationSuccessHandler() {
return new SimpleUrlAuthenticationSuccessHandler() {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
OidcUser oidcUser = (OidcUser) authentication.getPrincipal();
request.getSession().setAttribute("user", oidcUser);
System.out.println("User details: " + oidcUser.getAttributes());
response.sendRedirect("http://localhost:3000/");
}
};
}
}
the logincontroller looks like this
@RestController
public class LoginController {
@GetMapping("/api/login/oauth2/code/google")
public ResponseEntity<String> callback(OAuth2AuthenticationToken token, HttpServletRequest request) {
// Print request details
System.out.println("Request Method: " + request.getMethod());
System.out.println("Request URL: " + request.getRequestURL());
System.out.println("Request Headers: ");
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
System.out.println(headerName + ": " + request.getHeader(headerName));
}
if (token == null) {
System.out.println("Authentication token is missing or invalid.");
return ResponseEntity.badRequest().body("Authentication token is missing or invalid.");
}
OAuth2User user = token.getPrincipal();
String email = user.getAttribute("email");
String name = user.getAttribute("name");
System.out.println("User details: " + user.getAttributes());
return ResponseEntity.ok("Welcome, " + name + " (" + email + ")");
}
}
i have tried changing the routing to go directly to the service and not do any load balancing but i got the same result. i am very confused as to what the difference between running it in a docker container and running it normally is so it is hard for me to troubleshoot
本文标签: spring bootoauth2 with google not working in docker containerStack Overflow
版权声明:本文标题:spring boot - oauth2 with google not working in docker container - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736477683a1944333.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论