我有Spring引导Rest应用程序。最近,我使用JSONWebToken实现了用户身份验证。
@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private JwtAuthFilter authFilter;
@Autowired
private JwtAuthenticationProvider authenticationProvider;
@Autowired
private JwtAuthenticationEntryPoint authenticationEntryPoint;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
//http.csrf().ignoringAntMatchers("/token");
http.csrf().disable();
http.cors().and().authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.antMatchers("/token").permitAll()
.antMatchers("/rest/**").authenticated()
.and()
.addFilterBefore(authFilter, UsernamePasswordAuthenticationFilter.class)
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint);
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("*"));
configuration.setAllowedMethods(Arrays.asList("GET","POST","OPTIONS","PUT","DELETE"));
configuration.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/rest/**", configuration);
return source;
}
}
JWTauthFilter:
@Component
public class JwtAuthFilter implements Filter
{
@Override
public void doFilter(ServletRequest request, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
{
HttpServletRequest servletRequest = (HttpServletRequest) request;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String authorization = servletRequest.getHeader("Authorization");
String origin = servletRequest.getHeader("Origin");
if (authorization != null)
{
JwtAuthToken token = new JwtAuthToken(authorization.replaceAll("Bearer ", ""));
SecurityContextHolder.getContext().setAuthentication(token);
}
filterChain.doFilter(servletRequest, response);
}
@Override
public void destroy()
{
}
@Override
public void init(FilterConfig filterConfig) throws ServletException
{
}
}
我的登录控制器有一个映射“/token”,所有其他控制器都映射为“/rest/**”。有趣的是,调用一个“/token”控制器可以很好地工作...它返回令牌和用户详细信息。服务器响应包含:
当我改变
source.registerCorsConfiguration("/rest/**", configuration);
到source.registercorsconfiguration(“/**”,配置);
错误是access-control-allow-origin头http://localhost:4200和http://localhost:4200中有两个值
这是个老问题&我不确定你是否找到了答案。我只能建议调试这些问题的方法,因为这些问题通常是非常本地设置的。
1.仅有Java代码不足以调试CORS问题。您需要查看浏览器的网络选项卡并查看哪个调用失败--选项调用还是实际数据调用。其次,在浏览器中,需要仔细查看请求和响应头,以确定是否有失败的请求&这些请求需要在下面列出。下面这些。
在请求端-起源
当我使用postman测试它时,似乎一切都运行得很好,但当从Angular Application调用其馀部分时,我在引用CORS时出现错误:
这是正常的行为,因为CORS是浏览器的安全功能,您可能不会在任何其他客户端中看到此问题。CORS错误并不意味着您的API调用没有成功,它只意味着browser正在抑制响应&没有向前移动。
因此,如果您的API使用者不是web浏览器,那么您很可能在API端与CORS无关。其他客户机不是通过包含options
请求来使其成为一个两步过程。
如何对特定url禁用Spring Security性
因此,找到CORS问题的理想方法是通过接收有问题的请求来调试代码&验证响应头access-control-allow-origin
是否正确填充。
exVim 的配色由三部分组成: 你自己的Vim配色, exVim 插件的语法高亮和插件的配色. 你可以按照以下步骤来定制你的配色: 安装你的配色 exVim 提供了三种方法安装你的自定义配色 方法1. 在 ex-colorscheme 中安装(推荐) 首选的方法是在 ex-colorschemes 中安装自己的配色, 这种方法仅仅需要你把自己的配色文件放到 vimfiles/bundle/ex-
目录: 在配置项目yml文件中: 问题: null 客户端YML: 有没有人知道我怎样才能在这两种情况下只带一个配置文件?
丰富的过滤器插件的存在是 logstash 威力如此强大的重要因素。名为过滤器,其实提供的不单单是过滤的功能。在本章我们就会重点介绍几个插件,它们扩展了进入过滤器的原始数据,进行复杂的逻辑处理,甚至可以无中生有的添加新的 logstash 事件到后续的流程中去!
Codec 是 logstash 从 1.3.0 版开始新引入的概念(Codec 来自 Coder/decoder 两个单词的首字母缩写)。 在此之前,logstash 只支持纯文本形式输入,然后以过滤器处理它。但现在,我们可以在输入 期处理不同类型的数据,这全是因为有了 codec 设置。 所以,这里需要纠正之前的一个概念。Logstash 不只是一个input | filter | outpu
在 “Hello World” 示例中,我们已经见到并介绍了 logstash 的运行流程和配置的基础语法。从这章开始,我们就要逐一介绍 logstash 流程中比较常用的一些插件,并在介绍中针对其主要适用的场景,推荐的配置,作一些说明。 限于篇幅,接下来内容中,配置示例不一定能贴完整。请记住一个原则:Logstash 配置一定要有一个 input 和一个 output。在演示过程中,如果没有写明
根据文档--不管应用程序名称如何,如果模式与*/development(即localhost:8888/user/development或localhost:8888/demo/development)匹配,配置服务器应该匹配配置文件模式并获取适当的属性。例如:http://localhost:8888/demo/development我应该从ssh://git@xxxgithub.com/dev