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

当您登录时,您将被重定向回登录页面

梁丘经艺
2023-03-14

我刚开始研究Spring安全,遇到了一个问题。在登录表单中输入凭据后,用户将被重定向回登录页。

下面是我配置的SecurityConfiguration.java:

@Configuration
@EnableWebSecurity
public class SpringSecurityConfiguration extends WebSecurityConfigurerAdapter {

private final PasswordEncoder passwordEncoder;
private final UserDetailsService userDetailsService;

public SpringSecurityConfiguration(PasswordEncoder passwordEncoder,
                                   @Qualifier("userDetailsServiceImpl") UserDetailsService userDetailsService) {
    this.passwordEncoder = passwordEncoder;
    this.userDetailsService = userDetailsService;
}

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

    http.httpBasic().disable()
            .csrf().disable()
            .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .authorizeRequests().antMatchers("/registration").permitAll()
            .and()
            .authorizeRequests().anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .usernameParameter("login")
            .passwordParameter("password")
            .permitAll()
            .defaultSuccessUrl("/main")
            .and()
            .logout()
            .logoutSuccessUrl("/login")
    ;
}

@Override
protected void configure(AuthenticationManagerBuilder auth) {
    auth.authenticationProvider(daoAuthenticationProvider());
}

@Bean
protected DaoAuthenticationProvider daoAuthenticationProvider() {
    DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
    daoAuthenticationProvider.setPasswordEncoder(passwordEncoder);
    daoAuthenticationProvider.setUserDetailsService(userDetailsService);
    return daoAuthenticationProvider;
}

}

@GetMapping("/login")
public String showLoginForm(Model model) {
    model.addAttribute("user",new LoginForm());
    return "login";
}

登录表单:

 <!DOCTYPE html>
 <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
 <head>
     <meta charset="UTF-8">
     <title>Login</title>
 </head>
 <body>
 <form th:action="@{/login}" th:object="${user}" method="post">
      <div>
           <label>
              <input type="text" th:field="*{login}" placeholder="Login">
           </label>
      </div>
      <div>
           <label>
               <input type="text" th:field="*{password}" placeholder="Password">
           </label>
      </div>
           <button class="primary-button" type="submit">LOGIN</button>
           <a th:href="@{/registration}">Add new user</a>
 </form>
 </body>
 </html>

共有1个答案

杜俭
2023-03-14

我在一个大学项目中也有同样的问题。我们和我们的老师谈了很长时间,几个小时后,他无法解决这个问题,我们做出了一个变通办法,使用第二个按钮,登录后重定向到下一页。

因此,真正的登录按钮是检查凭据,第二个按钮是将标识令牌添加到cookie,然后通过按第二个按钮传递cookie。

 类似资料:
  • 记录器文件中的日志- org.springframework.Security.Access.event.loggerlistener-安全授权失败,原因是:org.springframework.Security.Access.accessdeniedexception:访问被拒绝;通过身份验证的主体:org.springframework.security.authentication.ano

  • 问题内容: 我正在做一个简单的论坛,由一系列论坛组成,每个论坛分别代表首页,主题,postitit,登录名和用户列表页面。在某些页面上,当用户未登录时会出现一个链接。 我想要实现的是在登录后触发重定向(在RequestDispatcher上使用forward()),以便浏览器返回到用户在单击登录链接之前所在的页面。为此,我看到了两种解决方案。 第一种解决方案是使用带有登录按钮的HTML 和一个不可

  • 问题内容: 我的应用看起来像: 如果用户访问/ game下的页面并没有登录,我想将他们重定向到登录页面。 如何在所有路由器中做到优雅? 问题答案: 基本思想是使用自定义组件(在下面的示例中为PrivateRoute)包装需要身份验证的路由。PrivateRoute将使用某种逻辑来确定用户是否已通过身份验证,然后确定是否通过身份验证。允许请求的路由呈现或重定向到登录页面。 在react-router

  • 我在Laravel5.2中使用了php artisan函数。 我想重定向的客人登录页面,若客人点击链接,只有用户并没有客人。 我想在登录后将用户重定向到后面的页面。 我怎么能这么做?请详细展示一些文件名示例。 ///////编辑 路线 控制器 Kernel.php

  • 我有一个文件,带有一个,具有和字段,在上,它用

  • 在我的keycloak客户端设置中,我向Web Origins配置部分添加了一个值'*'。 我还使用node cors库在我的node express应用程序上启用了cors,请遵循以下express指南 我使用keycloak的3.2.1版本,以防有任何不同(我看到一个新版本是作为RC发布的) 谢谢