使用Spring自定义登录/注册表单 - 无法将用户重定向到个人资料页面

时间:2017-07-12 15:46:44

标签: spring spring-mvc spring-security

我一直在使用spring登录和注册表单,我被卡住了一点点。我的登录和注册表单是自定义的,位于同一页面上。

loginRegister.html

 <form role="form" action="/app/register" method="post" />                          
      <input type="text" name="firstname" placeholder="First name..."  />                          
      <input type="text" name="lastname" placeholder="Last name..." />                       
      <input type="text" name="username" placeholder="Username"  />
      <input type="text" name="email" placeholder="Email @" />
      <input type="text" name="password" placeholder="Password" />
      <button type="submit" class="btn">Sign me up!</button>
                                </form>


  <form role="form" action="/app/login" method="post" >
      <input type="text" name="username" placeholder="Username..." />
      <input type="password" name="password" placeholder="Password..."/> 
      <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> 
       <button type="submit" class="btn">Sign in!</button>
                                </form>

HomeController中:

@Controller
@Transactional
@RequestMapping({"/", "/homepage"})
public class HomeController {

    @RequestMapping(path = "/login", method = RequestMethod.GET)
        public String showLoginRegister() {
            return "login";
        }

        @RequestMapping(path="/register", method=RequestMethod.POST)
        public String register(User user){
            userRepository.save(user);
            return "redirect:/users/" + user.getUsername();
        }

        @RequestMapping(path="/login", method=RequestMethod.POST)
        public String login (User user)
        {
            return "redirect:/users/" + user.getUsername();
        } 

UserController中:

@Controller
@RequestMapping("/users")
public class UserController {

    private UserRepository userRepository;

    @Autowired
    public UserController(UserRepository userRepository){
        this.userRepository=userRepository;
    }


     @RequestMapping(value="/{username}", method = RequestMethod.GET)
        public String showUserProfile(@PathVariable String username, Model model){
            User user = userRepository.findByUsername(username);
            model.addAttribute(user);
            return "userProfile";
        }


}   

当我的用户注册时,它应该被重定向到个人资料页面,同样是他/她登录后。

我的Spring安全类是:

SecurityWebApplicationInitializer类

public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer{

}

SecurityConfig类

@Configuration
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter
{

    @Autowired
    private DataSource dataSource;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
          .jdbcAuthentication()
            .dataSource(dataSource);
            //.passwordEncoder(bcryptEncoder); 
    }

    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers("/login").permitAll()
            .antMatchers("/admin").hasRole("ADMIN")
                .anyRequest().authenticated()
                .and()
            .formLogin()
                  .loginPage("/login")
                .and()
            .httpBasic();

        http.csrf().disable();
      }
}

我的问题是用户永远不会从loginRegister页面重定向到个人资料页面。

1 个答案:

答案 0 :(得分:0)

使用代码

更新SecurityConfig文件
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
        .antMatchers("/login").permitAll()
        .antMatchers("/admin").hasRole("ROLE_ADMIN")
            .anyRequest().authenticated()
            .and()
        .formLogin()
              .loginPage("/login")
            .and()
        .httpBasic();

    http.csrf().disable();
  }