Java Spring Security不断将我重定向到登录页面

Java Spring Security不断将我重定向到登录页面,java,spring-mvc,spring-boot,spring-security,Java,Spring Mvc,Spring Boot,Spring Security,我在地址栏中输入的任何链接都会将我重定向到登录页面。我怎样才能防止呢 例如,如果我添加>它会将我重定向到 ,因此,如果我在之后添加任何内容,我将重定向到帐户/登录视图 我的安全配置: package com.example.configuration; 导入javax.sql.DataSource; 导入org.springframework.beans.factory.annotation.Autowired; 导入org.springframework.beans.factory.annot

我在地址栏中输入的任何链接都会将我重定向到登录页面。我怎样才能防止呢

例如,如果我添加>它会将我重定向到 ,因此,如果我在之后添加任何内容,我将重定向到帐户/登录视图

我的安全配置:

package com.example.configuration;
导入javax.sql.DataSource;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Value;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
导入org.springframework.security.config.annotation.web.builders.HttpSecurity;
导入org.springframework.security.config.annotation.web.builders.WebSecurity;
导入org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
导入org.springframework.security.config.annotation.web.configuration.websecurityConfigureAdapter;
导入org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
导入org.springframework.security.web.util.matcher.AntPathRequestMatcher;
@配置
@启用Web安全性
公共类安全配置扩展了WebSecurity配置适配器{
@自动连线
专用BCryptPasswordEncoder BCryptPasswordEncoder;
@自动连线
私有数据源;
@值(${spring.querys.users query}”)
私有字符串用户查询;
@值(${spring.querys.roles query}”)
私有字符串角色查询;
@凌驾
受保护的无效配置(AuthenticationManagerBuilder身份验证)
抛出异常{
认证
.jdbc身份验证()
.usersByUsernameQuery(usersQuery)
.authoritiesByUsernameQuery(角色查询)
.dataSource(数据源)
.密码编码器(bCryptPasswordEncoder);
}
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
http
.授权请求()
.antMatchers(“/”).permitAll()
.antMatchers(“/index”).permitAll()
.antMatchers(“/other/other”).permitAll()
.antMatchers(“/account/login”).permitAll()
.antMatchers(“/account/registration”).permitAll()
.antMatchers(“/account/admin/**”).hasAuthority(“admin”)
.anyRequest().authenticated()
.及()
.csrf().disable()
.formLogin()
.loginPage(“/account/login”)
.failureUrl(“/account/login?error=true”)
.defaultSuccessUrl(“/account/admin/”)
.usernameParameter(“电子邮件”)
.passwordParameter(“密码”)
.及()
.logout().permitAll()
.logoutRequestMatcher(新的AntPathRequestMatcher(“/logout”))
.logoutSuccessUrl(“/”)
.及()
.例外处理()
.accessDeniedPage(“/访问被拒绝”);
}
@凌驾
public void configure(WebSecurity web)引发异常{
网状物
.忽略()
.antMatchers(“/resources/**”,“/static/**”,“/css/**”,“/js/**”,“/images/**”,“/img/**”);
}
}

您已配置所有其他URL必须经过身份验证,请参阅:

授权请求

我们的示例只要求对用户进行身份验证,并且对应用程序中的每个URL都这样做。我们可以通过向
http.authorizeRequests()
方法添加多个子项来指定URL的自定义要求。例如:

protected void configure(HttpSecurity http) throws Exception {
  http
      .authorizeRequests()                                                          1
          .antMatchers("/resources/**", "/signup", "/about").permitAll()            2
          .antMatchers("/admin/**").hasRole("ADMIN")                                3
          .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")      4
          .anyRequest().authenticated()                                             5
          .and()
      // ...
      .formLogin();
}
1 http.authorizeRequests()方法有多个子项,每个匹配器按声明顺序考虑

2 我们指定了任意用户都可以访问的多个URL模式。具体来说,如果URL以“/resources/”、等于“/signup”或等于“/about”开头,则任何用户都可以访问请求

3 任何以“/admin/”开头的URL将仅限于具有“role\u admin”角色的用户。您会注意到,由于我们调用的是hasRole方法,因此不需要指定“ROLE_u2;”前缀

4 任何以“/db/”开头的URL都要求用户同时具有“ROLE\u ADMIN”和“ROLE\u DBA”。您会注意到,由于我们使用的是hasRole表达式,因此不需要指定“ROLE_u2;”前缀

5 任何尚未匹配的URL只需要对用户进行身份验证


尝试使用
127.0.0.1
而不是
localhost

解释

您的浏览器没有为
localhost
(请参阅)设置
JSESSIONID
cookie,因此在成功登录后重定向时,下一个请求看起来未通过服务器的身份验证

如果您使用idea并单击“服务”面板打开浏览器,您可以在
application-dev.yml
中添加此配置以修复此问题:

服务器:
地址:127.0.0.1

我在索引页上有一个链接,可以使用thymeleaf转到注册表:单击此链接可将我重定向到帐户/login@dur是的/索引工作,但似乎我不能点击链接。。。是否存在阻止自动重定向到登录页面的方法?