2016-05-12 6 views
3

私のアプリケーションにログインしようとすると多少厄介な問題があります。アプリケーションがきれいにロードし、/loginページに行く春のセキュリティ - どのように私のアプリケーションにログインできないのですか?

protected void configure(HttpSecurity http) throws Exception { 
    System.out.println(http); 
    http 
     .formLogin() 
      .loginPage("/login") 
      .usernameParameter("ssoId") 
      .passwordParameter("password") 
      .and() 

     .authorizeRequests() 
      // I admit that this section needs some work 
      .antMatchers("/", "/home/*", "/alert/*", "/scheduler/*", "/agent/*", "/ftp/*", "/smtp/*", "/sql/*").access("hasRole('USER')") 
      .antMatchers("/benefit/*", "/client/*", "/contract/*", "/role/*", "/structure/*", "/term/*").access("hasRole('USER')") 
      .antMatchers("/", "/home/*", "/alert/*", "/scheduler/*", "/agent/*", "/ftp/*", "/smtp/*", "/sql/*").access("hasRole('ADMIN')") 
      .antMatchers("/benefit/*", "/client/*", "/contract/*", "/role/*", "/structure/*", "/term/*").access("hasRole('ADMIN')") 
      .antMatchers("/admin/**").access("hasRole('ADMIN')") 
      .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')") 

      .and() 
       .rememberMe().rememberMeParameter("remember-me").tokenRepository(persistentTokenRepository()).tokenValiditySeconds(86400) 
      .and() 
       .csrf() 
      .and() 
       .exceptionHandling().accessDeniedPage("/accessDenied"); 
} 

私の春のセキュリティprotected void configure(HttpSecurity http) throws Exceptionは次のように定義されます。しかし、ユーザーmasterとログオンして正しいパスワードを入力すると、単に/loginページに戻ります。

マイログインコントローラは次のとおりです。

@Controller 
public class LoginController { 

    @Autowired 
    UserProfileService userProfileService; 

    @Autowired 
    UserService userService; 

    @RequestMapping(value = { "/", "/home", "/welcome" }, method = RequestMethod.GET) 
    public String index(Principal principal) { 
     return principal != null ? "home/homeSignedIn" : "home/homeNotSignedIn"; 
    } 

    @RequestMapping(value = "/login") 
    public String loginPage() { 
     return "login"; 
    } 

    @RequestMapping(value="/logout", method = RequestMethod.GET) 
    public String logoutPage (HttpServletRequest request, HttpServletResponse response) { 
     Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
     if (auth != null){  
      new SecurityContextLogoutHandler().logout(request, response, auth); 
     } 
     return "redirect:/login?logout"; 
    } 

    private String getPrincipal(){ 
     String userName = null; 
     Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 

     if (principal instanceof UserDetails) { 
      userName = ((UserDetails)principal).getUsername(); 
     } else { 
      userName = principal.toString(); 
     } 
     return userName; 
    } 

    @ModelAttribute("roles") 
    public List<UserProfile> initializeProfiles() { 
     return userProfileService.findAll(); 
    } 
} 

User

@Entity 
@Table(name="user") 
public class User extends BasePojo { 

    @NotEmpty 
    @Column(name="sso_id", unique=true, nullable=false) 
    private String ssoId; 

    @NotEmpty 
    @Column(name="password", nullable=false) 
    private String password; 

    @NotEmpty 
    @Column(name="first_name") 
    private String firstName; 

    @NotEmpty 
    @Column(name="last_name") 
    private String lastName; 

    @Column(name="email", nullable=false) 
    private String email; 

    @Column(name="state", nullable=false) 
    private String state=UserState.ACTIVE.getState(); 

    @ManyToMany(fetch = FetchType.EAGER) 
    @Fetch(FetchMode.SELECT) 
    @JoinTable(name = "hrm_user_user_profile", 
     joinColumns = { @JoinColumn(name = "id_user", referencedColumnName="id") }, 
     inverseJoinColumns = { @JoinColumn(name = "id_profile", referencedColumnName="id") }) 
    @Cascade(org.hibernate.annotations.CascadeType.ALL) 
    private Set<UserProfile> userProfiles; 

UserProfile

@Entity 
@Table(name="user_profile") 
public class UserProfile extends BasePojo { 

private static final long serialVersionUID = 1L; 

    @Column(name="type", length=15, unique=true, nullable=false) 
    private String type = UserProfileType.USER.getUserProfileType(); 

    // Constructor used only for initial data loading, not used after 
    public UserProfile() { 
    } 

    // Constructor used only for initial data loading, not used after 
    public UserProfile(String type) { 
     super(); 
     this.type = type; 
    } 

    public String getType() { 
     return type; 
    } 

    public void setType(String type) { 
     this.type = type; 
    } 
} 

UserState

public enum UserState { 

    LOCKED("state.locked"), 
    INACTIVE("state.inactive"), 
    ACTIVE("state.active"); 

    String state; 

    private UserState(final String state){ 
     this.state = state; 
    } 

    public String getState(){ 
     return state; 
    } 

私はここで迷っています。助けてもらえますか?

+1

Spring SecurityのDEBUGロギングを有効にしましたか?それは何と言いますか? – holmis83

+0

私はDEBUGのロギングを有効にすることができましたが、スタックトレースが多すぎることに圧倒されて、どこに問題があるか把握しようとしています。 – gtludwig

+0

2つの出力が私の注意を引っ張った: 'SecurityContextHolderはすでに匿名トークンを含んでいないので、 'org.sprin[email protected]9055e4a6:Principal:anonymousUser;資格:[プロテクト];認証済み:真;詳細:org.sprin[email protected]957e:RemoteIpAddress:127.0.0.1; SessionId:ACF7917E21FC1061F792C959AD0E1323;許可された権限:ROLE_ANONYMOUS'' – gtludwig

答えて

2

私はあなたが正しく認証されていると信じています。ただし、/ login POSTハンドラメソッドでは、後続のリクエストにヘッダーにクッキーまたはトークンが含まれていることを確認する必要があります。

この方法では、以下のようにあなたが設定したルールは、例えば

 .antMatchers("/", "/home/*", "/alert/*", "/scheduler/*", "/agent/*", "/ftp/*", "/smtp/*", "/sql/*").access("hasRole('USER')") 
     .antMatchers("/benefit/*", "/client/*", "/contract/*", "/role/*", "/structure/*", "/term/*").access("hasRole('USER')") 
     .antMatchers("/", "/home/*", "/alert/*", "/scheduler/*", "/agent/*", "/ftp/*", "/smtp/*", "/sql/*").access("hasRole('ADMIN')") 
     .antMatchers("/benefit/*", "/client/*", "/contract/*", "/role/*", "/structure/*", "/term/*").access("hasRole('ADMIN')") 
     .antMatchers("/admin/**").access("hasRole('ADMIN')") 
     .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')") 

を適用され、ユーザーがアクセス/家庭しようとしていると言うことができます。ユーザーは認証されていないため、ログインページにリダイレクトされます。ユーザはユーザ名とパスワードを入力し、今認証されます。これが完了すると、デフォルトでスプリングセキュリティがユーザをリターンURL/homeにリダイレクトします。デフォルトでは、バネセキュリティはレスポンスにクッキーを追加します。その結果、すべての後続のリクエストにリクエスト内のそのクッキーが含まれます。私はこれがあなたの場合に起こっていないと信じています。私はあなたの春のセキュリティ設定についてもっと知りたいと思います。ユーザー認証はどのように行われますか?それはメモリ認証ですか?またはデータソース認証?

+0

ありがとう、バディ!私はすでに問題を解決していました。私の(再)春の曲線の間、私はunadvertdlyハードコードされたユーザーのステータスを設定し、私はそれをリファクタリングしようとすると、私は迷子になった。私はあなたの答えを受け入れました。 – gtludwig

+1

あなたは大歓迎です@gtludwig :) – koder23

+1

私は全く同じ問題がありました。将来の読者のために、私は 'UserDetails#isAccountNonLocked'メソッドにロジックバグを持っていました。だから、私の 'UserDetailsS​​ervice' impl *は私のユーザオブジェクトを正しく見つけていました。*パスワードの一致は*成功しました。しかし、ある時点では、返されようとしているユーザ/プリンシパルそれらは 'isAccountNonLocked'メソッドです。私のバグは、そのメソッドを呼び出すときに誤って 'false'を返していました。これは' isAccountLocked == true'と同じです...それはダブルネガティブなバグでした。 – smeeb

関連する問題