提交 80810f3e authored 作者: Matrix's avatar Matrix

[系统架构] 修正跨域的 Credentials 认证问题

上级 4f165541
...@@ -12,6 +12,7 @@ import com.tykj.dev.device.user.config.url.UrlFilterInvocationSecurityMetadataSo ...@@ -12,6 +12,7 @@ import com.tykj.dev.device.user.config.url.UrlFilterInvocationSecurityMetadataSo
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.ObjectPostProcessor; import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
...@@ -92,6 +93,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { ...@@ -92,6 +93,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
configuration.setAllowedMethods(methodNames); configuration.setAllowedMethods(methodNames);
configuration.setAllowedHeaders(Arrays.asList("Origin", "X-Requested-With", "Content-Type", "Accept")); configuration.setAllowedHeaders(Arrays.asList("Origin", "X-Requested-With", "Content-Type", "Accept"));
configuration.setMaxAge(3600L); configuration.setMaxAge(3600L);
configuration.setAllowCredentials(true);
configuration.addExposedHeader("Location"); configuration.addExposedHeader("Location");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration); source.registerCorsConfiguration("/**", configuration);
...@@ -105,6 +107,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { ...@@ -105,6 +107,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
.cors() .cors()
.and() .and()
.authorizeRequests() .authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/** ").permitAll()
.withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() { .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
@Override @Override
public <O extends FilterSecurityInterceptor> O postProcess(O o) { public <O extends FilterSecurityInterceptor> O postProcess(O o) {
......
package com.tykj.dev.device.user.config.handler; package com.tykj.dev.device.user.config.handler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler; import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -13,10 +14,12 @@ import java.io.IOException; ...@@ -13,10 +14,12 @@ import java.io.IOException;
* @author zjm * @author zjm
*/ */
@Component @Component
@Slf4j
public class MyFailHandler implements AuthenticationFailureHandler { public class MyFailHandler implements AuthenticationFailureHandler {
@Override @Override
public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException { public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
log.error("[登录模块] 登录发生异常 : {}", e.toString());
httpServletResponse.setStatus(200); httpServletResponse.setStatus(200);
httpServletResponse.setCharacterEncoding("utf-8"); httpServletResponse.setCharacterEncoding("utf-8");
httpServletResponse.setContentType("application/json"); httpServletResponse.setContentType("application/json");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论