2017-03-13 13 views
0

私は春のセキュリティに新しいですし、私はそれに少し問題があります。春mvc +春のセキュリティ、ログイン後の404

だから私は2つのモデルクラスを持っている:

ユーザー:

@Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long id; 
    private String firstName; 
    private String lastName; 
    @NotEmpty 
    private String email; 
    @NotEmpty 
    private String password; 
    @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER) 
    private Set<UserRole> roles = new HashSet<>(); 

のUserRole:

@Id 
    @GeneratedValue 
    private Long id; 
    private String role; 
    private String description; 

登録ユーザの瞬間には、デフォルトでROLE_USERが割り当てられます。

私は自分のUserDetailsServiceの実装を作っ:

@Autowired 
    private UserRepository userRepository; 

    @Override 
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 
     User user = userRepository.findByEmail(username); 
     if (user == null) 
      throw new UsernameNotFoundException("User not found"); 
     return new org.springframework.security.core.userdetails.User(
       user.getEmail(), 
       user.getPassword(), 
       convertAuthorities(user.getRoles()) 
     ); 
    } 

    private Set<GrantedAuthority> convertAuthorities(Set<UserRole> userRoles) { 
     Set<GrantedAuthority> authorities = new HashSet<>(); 
     for (UserRole ur : userRoles) { 
      authorities.add(new SimpleGrantedAuthority(ur.getRole())); 
     } 
     return authorities; 
    } 

そして、私のセキュリティ設定:

@Autowired 
    private UserDetailsService userDetailsService; 

    @Bean 
    public BCryptPasswordEncoder bCryptPasswordEncoder() { 
     return new BCryptPasswordEncoder(); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests() 
       .antMatchers("/css/**", "/register").permitAll() 
       .anyRequest().authenticated() 
       .and() 
       .formLogin().loginPage("/login").permitAll().usernameParameter("email").passwordParameter("password") 
       .and().logout().logoutSuccessUrl("/").invalidateHttpSession(true).and().csrf(); 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder()); 
    } 

登録は/registerコントローラでsuccesfullのです。

同じ資格情報でログインしたい場合は、問題が発生します。ログインフォームから私は/loginコントローラにPOSTリクエストを送信しています。

フォーム:

<form class="form-signin" action="#" th:action="@{/login}" method="post"> 
     <h2 class="form-signin-heading">Please log in</h2> 
     <label for="inputEmail" class="sr-only">Email address</label> 
     <input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" th:required="required"></input> 
     <label for="inputPassword" class="sr-only">Password</label> 
     <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" th:required="required"></input> 
     <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button> 
</form> 

/loginコントローラ:

@PostMapping("/login") 
    public String processLogin(){ 
     logger.debug("processLogin"); 
     return "secure"; 
} 

/loginコントローラはsecureページを返す必要がありますが、私が言ったように代わりに私が404エラー

Whitelabel Error Page 

This application has no explicit mapping for /error, so you are seeing this as a fallback. 

Mon Mar 13 21:17:02 CET 2017 
There was an unexpected error (type=Not Found, status=404). 
No message available 

を受けています、私は春の安全保障で新しいです、そして、私は間違っていません...

+0

"secure"はloginと同じフォルダにある通常のHTMLページです –

+0

web.xmlやspring config xmlファイルのような詳細を入力してください。あなたは404を取得している、それはまったくコントローラに到達していないことを意味します。 –

+1

これは春のブートプロジェクトなので、私は設定ファイルを持っていません。 –

答えて

0

春のセキュリティでauthentication-failure-urlを提供する必要があります。formLogin().loginPage("/login").failureUrl("/loginPage?error")をセキュリティ設定で追加してください。

+0

それは役に立たなかった。 –

+0

同じエラーログ '404'が来ていますか違うのですか? – hrdkisback

+0

同じですが、何も変わっていませんでした。 –

関連する問題