Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Spring自定义登录/注册表单-无法将用户重定向到配置文件页面_Spring_Spring Mvc_Spring Security - Fatal编程技术网

使用Spring自定义登录/注册表单-无法将用户重定向到配置文件页面

使用Spring自定义登录/注册表单-无法将用户重定向到配置文件页面,spring,spring-mvc,spring-security,Spring,Spring Mvc,Spring Security,我一直在用spring处理登录和注册表单,我有点卡住了。我的登录和注册表格是自定义的,并且在同一页上 loginRegister.html <form role="form" action="/app/register" method="post" /> <input type="text" name="firstname" placeholder="First name..." />

我一直在用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>
用户控制器:

@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();
        } 
@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 security课程包括:

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页面重定向到profile页面

使用以下代码更新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();
  }