2 qq 35877352 qq_35877352 于 2016.09.13 15:35 提问

基于springboot,springsecurity的跨域问题

添加了Filter解决跨域问题配置:

 @Component
public class SimpleCORSFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String origin = (String) servletRequest.getRemoteHost()+":"+servletRequest.getRemotePort();
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization");
        response.setHeader("Access-Control-Allow-Credentials","true");
        filterChain.doFilter(servletRequest, servletResponse);
    }
    @Override
    public void destroy() {}
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}
}

但是这个配置似乎被springsecurity给拦截了并没有成功添加上去,当我给url设置忽略权限限制时( /order/** ):

   @Override
    public void configure(HttpSecurity http) throws Exception{
        http.authorizeRequests()
                .antMatchers("/order/**").permitAll()
                .anyRequest().authenticated();
        http.csrf()
                .disable()
                .formLogin()
                .loginPage("/login")
                .usernameParameter("username")
                .passwordParameter("password")
                .loginProcessingUrl("/login")
                .defaultSuccessUrl("/index")
                .failureUrl("/loginError")
                .permitAll();
    }

此时跨域配置是可以正常生效的,但是如果删除上面的/order/**,就会出现415的跨域问题,why?

2个回答

dabocaiqq
dabocaiqq   2016.10.08 01:47
linanmaxia
linanmaxia   2017.04.23 12:25

.antMatchers("/order/**").permitAll()表示放过/order之下的所有路径,这些路径可以直接访问而不会被拦截

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!