【问题标题】:How to make one rest endpoint accessible without any security , while having spring-security implemented in spring boot application如何在没有任何安全性的情况下使一个休息端点可访问,同时在 Spring Boot 应用程序中实现 spring-security
【发布时间】:2019-07-10 09:56:56
【问题描述】:

我需要在没有任何身份验证的情况下使一个休息端点全局可用,基于第一个服务的响应将准备令牌并将基于第二个服务的身份验证。由于具有 spring-security,应用程序总是要求实现 BASIC 或 FORM 登录,我的应用程序中没有这个用例。

即使我在 websecurityconfig 中有这条路由到 PERMITALL,仍然需要一些身份验证才能联系应用程序

 http
    .authorizeRequests()
    .antMatchers("/v1/test").permitAll()
.anyRequest().authenticated(;

【问题讨论】:

  • @AA_PV,期待您的回答
  • 没有你的代码实现是不可能提供答案的
  • 你是如何允许这条路线的?请添加一些代码
  • @MaruthiAdithya 请查看更新的代码
  • @Spara 请查看更新的代码

标签: java spring spring-boot spring-security spring-session


【解决方案1】:

如果你能分享你的代码就更好了,这里是配置spring security的代码。 在以下代码中:具有 permitAll 的 antMatchers 无需身份验证即可访问,角色 ROLE_USER 的 antMatchers 可在用户登录后访问。

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .antMatchers("/", "/home", "/user/register").permitAll()
            .antMatchers("/user/**").access("hasRole('ROLE_USER')")
            .and()
        .formLogin()    
            .loginPage("/login")
            .failureUrl("/login?error")
            .defaultSuccessUrl("/dashboard")    
            .usernameParameter("username")
            .passwordParameter("password")
            .and()
        .logout()
            .logoutSuccessUrl("/login?logout")  
            .and()
        .exceptionHandling()
            .accessDeniedPage("/403");
}

【讨论】:

  • 会在没有任何身份验证的情况下访问/home吗?
  • 是的 /, home 和 /user/register 都无需任何身份验证即可访问
猜你喜欢
  • 2015-01-06
  • 2017-07-07
  • 2021-05-16
  • 1970-01-01
  • 2015-04-13
  • 2011-09-16
  • 1970-01-01
  • 2018-05-15
  • 2019-07-05
相关资源
最近更新 更多