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

Spring Boot重定向太多

郭弘方
2023-03-14

我正在使用Thymeleaf开发一个简单的登录表单

我试图通过将以下属性放入我的应用程序来禁用默认的安全登录屏幕。属性:安全性。基本的已启用=错误

并将以下配置添加到我的SecurityConfig中,以便除"/登录"和"/资源"之外的任何URL都将获得身份验证:

@Configuration 
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired 
    private UserRepository userRepository;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
        .authorizeRequests()
            .antMatchers("/resources/**").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
        .logout()
            .permitAll();
    }

我的LoginController是明确的:

@Controller
public class LoginController {

        @RequestMapping(value="/login", method=RequestMethod.GET)
        public String loadForm(Model model) {
            model.addAttribute("user", new User());
            return "redirect:/login";
        }

有人知道为什么会这样吗?

共有1个答案

越骏俊
2023-03-14

控制器捕获并重定向到同一url:

@RequestMapping(value="/login", method=RequestMethod.GET)
    public String loadForm(Model model) {
        model.addAttribute("user", new User());
        return "redirect:/login";
    }

此外,您的SecurityConfig定义了以下内容:

@Configuration 
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    protected void configure(HttpSecurity http) throws Exception {
    ...
    .loginPage("/login")
}

。loginPage()的javadoc说:

如果需要身份验证,则要重定向到的登录页面

因此,任何安全请求或直接访问/登录都将:

  • 由于,重定向到/login。登录页面(“/login”)在您的SecurityConfig

 类似资料:
  • 当使用Keycloak-js(版本:4.0.0)在Angular4中创建会话时,SSO不起作用 null 响应URL: http://localhost:3000/login/generic_oauth#state=wwxu1iywxtsevsxwcfzwhpz7opm63dbu5aombtmdjhe%3d&session_state=6ec8255b-ed4c-4399-951c-0241ce7

  • 当使用自定义重定向URL配置Spring security时,该URL使用基本URI模板变量,如下所示:https://docs.Spring.io/Spring-security/site/docs/5.2.12.release/reference/html/oauth2.html#oauth2client-auth-code-redirect-uri 应用程序进入了太多重定向的循环。 此配置工

  • 我使用Laravel 5.4的项目和场景是,如果没有任何创建然后在登录重定向到页面。 下面是我的路线页编码 目录结构是 下面是我的code 它重定向了太多次,我收到错误消息说。 我在这里做错了什么??

  • RedirectAttribute:可以在Controller中return时设置跳转方式为重定向(return “redirect:要跳转的视图”),也可以在Controller中传参时传入一个RedirectAttributes类型的值,再配合flush给request中的存入数据,并且在重定向时候避免数据丢失。

  • 这是我的应用程序属性文件: 这是我的配置类: 我做错了什么? 谢谢。

  • 问题内容: 我使用以下代码发出了一个简单的URL请求: 但是在最后一行,我收到“重定向次数过多错误”。如果说我的“网页”变量是google.com,那么它可以正常工作,但是当我尝试使用我的servlet的URL时,它将失败。看来我可以用以下方法调整重定向之后的次数(默认为20): 但是当我将其提高到100时,抛出错误肯定需要更长的时间,所以我知道它正在尝试。但是,指向我的servlet的URL在(