我正在尝试在我的项目中使用Spring Security,这里是代码:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// TODO Auto-generated method stub
//super.configure(auth);
//auth.inMemoryAuthentication().withUser("admin").password("1111").roles("USER");
auth
.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery("select username, password, 1 from users where username=?")
.authoritiesByUsernameQuery("select users_username, roles_id from roles_users where users_username=?")
.rolePrefix("ROLE_");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable();
http
.httpBasic();
http
.authorizeRequests()
.anyRequest().authenticated();
http
.authorizeRequests()
.antMatchers("/users/all").hasRole("admin")
.and()
.formLogin();
http
.exceptionHandling().accessDeniedPage("/403");
}
Run Code Online (Sandbox Code Playgroud)
这是问题所在:
想象一下,我们的数据库中有两个用户(一个有user角色,另一个有admin角色),一个是admin,第二个是用户,问题是我作为用户连接时(只有user角色)可以访问管理员资源(这不是预期的行为).
我认为这个查询中的问题:
"select username, password, 1 from users where username=?"
Run Code Online (Sandbox Code Playgroud)
据说那username是主键? …