【问题标题】:Spring boot security not authenticating when using @CrossOrigin使用 @CrossOrigin 时 Spring Boot 安全性未进行身份验证
【发布时间】:2017-05-10 09:47:31
【问题描述】:

我一直在努力解决如何让 Spring Security 工作并允许所有来源进入我的 REST-full 应用程序。我已经查找了问题并尝试了大多数建议,但似乎没有任何东西可以独立工作,它们可以正常工作,但不能一起工作。

我的控制器非常简单,因为我只是在做一些测试

@RestController
@CrossOrigin
public class TestController
{

    @RequestMapping(path="/test/{name}", method= RequestMethod.GET)
    public String test(@PathVariable final String name)
    {
        return "Welcome " + name + "!";
    }

    @RequestMapping(path="/private/{name}", method= RequestMethod.GET)
    public String privateTest(@PathVariable final String name)
    {
        return "Welcome private " + name + "!";
    }
}

我的安全配置如下所示:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
    @Override
    public void configure(HttpSecurity httpSecurity) throws Exception
    {
        httpSecurity.cors().and().authorizeRequests()
                .antMatchers("/*").hasRole("USER")
                .and()
                .formLogin();
    }
}

有人有什么建议吗?另外,当默认情况下不允许 javascript 执行 REST-full http 请求时,您通常如何创建安全的 restfull 服务?

【问题讨论】:

    标签: javascript java spring-boot spring-security


    【解决方案1】:
    .antMatchers("/**").hasRole("USER")
    // or anyRequest().hasRole("USER")
    

    有关蚂蚁比赛的更多信息,请参阅here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-21
      • 1970-01-01
      • 2020-02-09
      • 2017-07-07
      • 2019-03-13
      • 2014-07-06
      • 1970-01-01
      • 2013-05-09
      相关资源
      最近更新 更多