0
JDBC(mysql)を使用して簡単な認証と認可を設定しようとしています。ここでのinitのユーザーに使用してイムSQLは次のとおりです。Spring起動時のJDBC認証
INSERT INTO users(username,password,enabled)
VALUES ('admin','admin', true);
INSERT INTO user_roles (username, role)
VALUES ('admin', 'USER');
INSERT INTO user_roles (username, role)
VALUES ('admin', 'ADMIN');
データソースのプロパティ:
spring.datasource.password=xxxx
spring.datasource.url=jdbc:mysql://localhost:3306/spring
spring.datasource.username=xxx
セキュリティ設定:うまく作品に
class ApplicationSecurity extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource source;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/uploadfiles/**").access("hasRole('ADMIN')");
http.authorizeRequests().antMatchers("/fonts/**").permitAll();
http.authorizeRequests().antMatchers("/").permitAll();
http.authorizeRequests().anyRequest().fullyAuthenticated().and()
.formLogin().loginPage("/login").defaultSuccessUrl("/").failureUrl("/login?error").permitAll().and()
.logout().permitAll();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(source).
usersByUsernameQuery("select username,password,enabled from users where username=?").
authoritiesByUsernameQuery("select username, role from user_roles where username=?");
}
}
ログけど/ uploadfilesリンクを開こうとすると私は403の応答を得る管理者として。私はまた、役割が正しいかどうかをチェックしようとしました。私はこのコードを使用してそれらを確認:
Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>) SecurityContextHolder.getContext().getAuthentication().getAuthorities();
authorities.forEach(authority -> logger.info(authority.toString()));
また、私はそれが期待どおりに動作inMemoryAuthenticationを設定するので、私はそれがMySQLの設定で何かしなければならなかったと思いますとき。誰も私のコードで間違いを見ますか?
'role'カラムに権限( 'ADMIN')またはロール( 'ROLE_ADMIN')が含まれていますか? –
接頭辞を付けずに 'ADMIN'、 'USER'などです。 –