当前位置: 首页 > 知识库问答 >
问题:

Spring security要求URL登录,即使在Configure方法中不进行身份验证也是允许的

訾旭
2023-03-14

我正在使用Spring Boot和Spring Security。我有多个URL,我希望其中一些可以通过身份验证访问,而其中一些可以在没有身份验证的情况下访问。我以这种方式编写了Configure方法。但浏览器会将我重定向到登录页面,即使是允许在没有身份验证的情况下访问的URL。例如,当我尝试访问localhost:8080/emailExists时,我被要求登录。我会做错什么?

我的控制器类:

@RestController
class AppController {

  @Autowired
  private ShopSearchRepository searchRepository;

  @Autowired
  private UserRepository userRepository;

  @GetMapping("/emailExists")
  public Boolean emailExists(@RequestParam String email) {
     User user = userRepository.findByEmail(email);
  
     if (user == null) return false;
     else return true;
  } 

  @GetMapping("/ShopsSearch")
      List<Shop> search(@RequestParam String name) {
      return searchRepository.findByName(name);
  }

}

我的配置方法:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().authorizeRequests()
    .antMatchers("/ShopsSearch/").authenticated()
    .anyRequest().permitAll()
    .and()
    .httpBasic();
}

共有1个答案

苏俊友
2023-03-14

你需要知道绕过哪个URL

 @Override
        public void configure(HttpSecurity http) throws Exception {
           http.csrf().disable().authorizeRequests()
            .antMatchers(HttpMethod.POST,"/emailExists").permitAll() // allow without authentication
            .antMatchers(HttpMethod.POST, "/login").permitAll() // allow without authentication
            .antMatchers(HttpMethod.POST,"/newuser/*").permitAll() // allow without authentication
            .antMatchers(HttpMethod.GET,"/master/*").permitAll() // allow without authentication
             .antMatchers(HttpMethod.GET,"/exploreCourse").permitAll() // allow without authentication
            .anyRequest().authenticated() //all other need auth
        }
 类似资料:
  • 我正在Firebase后端的帮助下开发应用程序,并且我正在使用Firebase Auth登录我的应用程序。我已经完成了所有集成和所有事情,我的应用程序运行良好。 但我只希望与单个用户的单个会话,因为现在用单个用户Id,我可以通过多个设备登录。 因此,我想限制用户一次只能在一台设备上登录。 我正在使用自定义身份验证和用户名密码登录: 如果用户在另一台设备中使用相同的ID登录,我想显示“您已经登录到另

  • 问题内容: 我想在我的Django编码网站中添加一些Ajax -niceness。 在我的Django代码中,我使用装饰器标记哪个视图需要认证。未经身份验证的用户单击时的默认行为是将其重定向到登录页面,然后传递目标页面。 我在某些网站上看到的,并且真正喜欢的是,当用户单击一个指向仅限于登录用户的位置的链接时,他/她得到了一个弹出窗口(通过JavaScript),而不是重定向到登录页面,而是询问他/

  • 我想允许只访问一个特定的Rest点而不进行身份验证。 控制器: 如果将antMatchers保留如上所示,我可以禁用两个点的身份验证。但我的目标是只允许带有id参数的GET通过而不进行身份验证。我尝试了Controller.myPath+“?id=**”,但它不起作用。有什么想法吗?多谢了。

  • 我试图用注册时使用的凭证登录。Firebase已经有注册用户的条目。每次我试图登录它显示“登录不成功”,我没有看到任何代码问题。请帮帮忙。

  • 我已经在我的应用程序中配置了spring saml和spring security。我给出了不同的url模式来识别请求。如果我在app URL中附加/rest,那么它将创建带有基本身份验证的Spring Security上下文。如果我在应用程序URL中附加/saml,那么它将填充IDP登录页面并重定向到索引。成功登录后返回html。 但我被重定向到登录。再次使用html页面而不是索引。html。在

  • 使用,https://github.com/firebase/FirebaseUI-Android/tree/master/codelabs/chat作为登录的参考,我在键入时似乎遇到了问题 我只能键入Auth的提供者,而不能键入,为什么会这样,它提示我键入社会提供者。