【发布时间】:2016-10-18 23:31:32
【问题描述】:
我没有例外,但是即使用户没有指定角色,也会调用标有@Secured 的方法。我在 pom.xml 中使用 spring-boot-starter-security 1.3.5 但没有 Spring Boot autoconfig 或其他注释。
@RequestMapping(value={"/l"}, method=RequestMethod.GET)
@Secured({"ROLE_TORZSMOD"})
public String list() {
return "partnerList";
}
我的安全配置:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
DataSource dataSource;
@Autowired
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
PasswordEncoder encoder = new StandardPasswordEncoder();
auth.jdbcAuthentication()
.dataSource(dataSource)
.passwordEncoder(encoder);
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin().loginPage("/login").permitAll()
.and()
.logout().logoutSuccessUrl("/login?logout").permitAll()
.and()
.authorizeRequests()
.antMatchers("/resources/**").permitAll()
.anyRequest().authenticated();
}
}
来自调试日志的登录信息(ROLE_TORZSMOD 不在权限列表中):
2016-06-17 09:55:10,378 DEBUG o.s.s.w.a.i.FilterSecurityInterceptor - Previously Authenticated: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@d78c39df:
Principal: org.springframework.security.core.userdetails.User@65812e3: Username: pappt; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true;
AccountNonLocked: true; Granted Authorities: ROLE_BIZMOD; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@0:
RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: 7CE797647B43DEDCED00AF439F446FA1; Granted Authorities: ROLE_BIZMOD
【问题讨论】:
-
试试@PreAuthorize(hasRole('ROLE_TORZSMOD'))
-
因为这个 ROLE_ 前缀混乱而尝试了 hasAuthority。结果相同。
<security:authorize access="hasAuthority('ROLE_TORZSMOD')">在 JSP 中工作。@PreAuthorize("hasAuthority('ROLE_TORZSMOD')")没有。还将注解修改为@EnableGlobalMethodSecurity(prePostEnabled=true, securedEnabled=true) -
tyr
@EnableGlobalMethodSecurity(prePostEnabled=true)和hasRole而不是 hasAuthority -
试过了,没区别。
标签: spring-security