Javaの春のセキュリティを持つユーザーのために、当局を与える方法

アレックス:

私は、DBから一部のユーザーを取得し、それら当局を与えることを試みます。それらは私のユーザクラスフィールドです。

private String  username;
private String  email;
private String  password;
private String  role;

私は、文字列の役割で当局を与えたいです。私はこれを試してみました:

@Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
          .withUser("user1").password(passwordEncoder().encode("user1Pass"))
          .authorities("ROLE_USER");
}

しかし、私は、DBからユーザーを取得し、configureGlobal方法でそれらを使用する方法がわかりません。

chaoluo:

あなたは使用することができJdbcUserDetailsManagerDBからユーザーをロードするために

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.jdbcAuthentication()
           .dataSource({datasource})
           .usersByUsernameQuery("select username,password,1 "+ "from xxx " + "where username = ?")
           .authoritiesByUsernameQuery("select username, role "+ "from xxx " + "where username = ?")
}

usersByUsernameQuery手段で1が有効になっています。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=234297&siteId=1