2016-08-26 3 views
0

私は春のセキュリティでサービスを認証しようとしています。そのために私はフィルタを使用していますJPAとスプリングセキュリティによるカスタムログイン

これは私が使っているセキュリティ設定です:この設定では、すべてのリクエストをフィルタリングし、すべてを認証で許可しています。認証のために私はログインページを持っています。ログインフォームでは、電子メールには「username」、属性名には「password」をそれぞれ使用しています。

public class LoginAuthenticate extends WebSecurityConfigurerAdapter{ 

    @Autowired 
    @Qualifier("userDetailsService") 
    MyUserDetailService userDetailsService;  

    @Autowired 
    @Override 
    public void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); 
     System.out.println("reaching"); 
    }   

    @Override 
    protected void configure(HttpSecurity http) throws Exception{ 
     http.authorizeRequests() 
     .antMatchers("/resources/**").permitAll() 
     .anyRequest().authenticated() 
     .and() 
     .formLogin() 
      .loginPage("/login") 
      .usernameParameter("username") 
      .passwordParameter("password") 
      .defaultSuccessUrl("/success") 
      .failureUrl("/") 
      .permitAll() 
      .and() 
     .logout() 
      .permitAll().and().csrf().disable(); 
    } 
    @Bean 
    public PasswordEncoder passwordEncoder(){ 
     PasswordEncoder encoder = new BCryptPasswordEncoder(); 
     return encoder; 
    } 

これは、私はこれは私が私が電子メールを取得するためにリポジトリをautowiredている、オーバーライド私のUserDetailsServiceで次

をした私のプログラム構造

Structure of program

そしてUserDEtailServiceを実装するためでありますユーザ名として電子メールを使用してデータベースからアドレスとパスワードを取得した後、ユーザ名とパスワードを持つuserdetailsユーザオブジェクトを作成し、そのオブジェクトを返します。使用がログインページでユーザー名とパスワードを入力すると、データベースから取得した詳細情報を使用して認証することができるはずです。ユーザーの電子メールとパスワードは、dbから取得した電子メールとパスワードと一致する必要があります。

@Service("userDetailsService") 
public class MyUserDetailService implements UserDetailsService{ 
@Autowired 
IUsers userrepo; 

     @Override 
     public UserDetails loadUserByUsername(String username) 
       throws UsernameNotFoundException { 
      User user = userrepo.finduserByUserName(username); 
      if (user == null) { 
       return null; 
      } 
      List<GrantedAuthority> auth = AuthorityUtils 
        .commaSeparatedStringToAuthorityList("ROLE_USER"); 
      if (username.equals("admin")) { 
       auth = AuthorityUtils 
         .commaSeparatedStringToAuthorityList("ROLE_ADMIN"); 
      } 
      String password = user.getLoginPassword(); 
      String Email = user.getEmailAddress(); 
      System.out.println(password+ " "+ Email); 
      return new org.springframework.security.core.userdetails.User(Email,password,auth); 
} 
} 

私の認証機能はユーザーを評価していません!

ここで間違っていることを教えてもらえますか?デバッグにエラーはありません。私は認証機能に到達すれば慰めますが、到達しません。私はJPAでこのカスタムログインでインターネット上のすべての可能なものを試しましたが、私の努力は実りあるものになることはありませんでした。クラスLoginAuthenticateで

答えて

0


は、このクラスのクラスMyUserDetailService.loadUserByUsernameで

@Configuration  
@EnableWebSecurity 


を注釈を追加し、void configure(AuthenticationManagerBuilder auth)
@Autowiredを削除します。

から
変更

if (user == null) { 
     return null; 
} 

if (user == null) { 
     throw new UsernameNotFoundException ("User not found"); 
} 
+1

しかし、私はすでにそれをしているので、それは私を助けません。私はそれを短くするためにここにそれを含めなかった。しかし、2番目の提案 "私はそれを取る"。ありがとう – venkatamangina

関連する問題