【发布时间】:2017-08-20 12:50:07
【问题描述】:
我正在尝试在我的项目中使用 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");
}
问题来了:
假设我们的数据库中有两个用户(一个具有user 角色,另一个具有admin 角色)一个管理员,第二个是用户,问题是当我以用户身份连接时(有只有user 角色)它可以访问管理资源(这不是预期的行为)。
我认为这个查询的问题:
"select username, password, 1 from users where username=?"
根据username是主键?
如果有人知道我该如何解决这个问题?
【问题讨论】:
标签: java spring spring-security