【发布时间】:2015-05-11 16:07:54
【问题描述】:
我使用带有嵌入式 Tomcat 和 Spring Boot Starter Security 的 Spring Boot 1.2.1。此外,我将 RestController 用于某些 Web 服务,并且我希望只有具有特定角色的某些用户才能访问 Web 服务。但它不起作用,安全不使用 RoleVoter 检查角色。在以下示例中,用户“user”可以访问网络服务,尽管他没有正确的角色!
首先我的应用配置
@Configuration
@EnableJms
@ImportResource( "classpath:net/bull/javamelody/monitoring-spring.xml" )
@EnableAspectJAutoProxy
@ComponentScan
@PropertySource( "classpath:application.properties" )
@EnableAutoConfiguration
@EnableGlobalMethodSecurity( securedEnabled = true )
public class ItemConfiguration { ... }
现在我的安全配置
@Configuration
@EnableWebSecurity
@Order( SecurityProperties.ACCESS_OVERRIDE_ORDER )
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure( AuthenticationManagerBuilder auth ) throws Exception {
auth.inMemoryAuthentication().withUser( "user" ).password( "password" ).roles( "USER" );
}
@Override
protected void configure( HttpSecurity http ) throws Exception {
http.authorizeRequests().anyRequest().fullyAuthenticated();
http.httpBasic();
http.csrf().disable();
}
}
休息控制器
@RestController
public class QueryController {
@Secured( { "ROLE_ADMIN" } )
@RequestMapping( value = "/", method = { POST }, consumes = { MediaType.APPLICATION_JSON_VALUE },
produces = MediaType.APPLICATION_JSON_VALUE )
ResponseEntity< List< BaseEntity > > query( @RequestBody @Valid final ItemQueryRequestData request )
throws Exception {
return new ResponseEntity<>( "", HttpStatus.OK );
}
}
application.properties
spring.data.mongodb.database = item
spring.data.mongodb.host = ${MONGODB_URI:pimpoc01}
spring.data.mongodb.port = ${MONGODB_PORT:27017}
spring.activemq.broker-url=${BROKER_URL:tcp://pimpoc01:61616}
spring.activemq.user=
spring.activemq.password=
spring.activemq.pooled=true
queue.item.in.channelId = item-in
queue.item.in.concurrentConsumers = 1
queue.item.in.destination = item-in
queue.itemOption.in.channelId = itemOption-in
queue.itemOption.in.concurrentConsumers = 1
queue.itemOption.in.destination = itemOption-in
queue.style.in.channelId = style-in
queue.style.in.concurrentConsumers = 1
queue.style.in.destination = style-in
queue.concurrentConsumers = 50
queue.dataCreation.response = dataCreationResponse
queue.structureAttributeValue.in.channelId = structureAttributeValue-in
queue.structureAttributeValue.in.concurrentConsumers = 1
queue.structureAttributeValue.in.destination = structureAttributeValue-in
validation.endpoint = ${VALIDATOR_URI:http://pimpoc01:8080/validator}
感谢您的帮助!
【问题讨论】:
-
你能发布你的application.properties的内容吗?也许你有一个属性,这会干扰这里。
-
@mathias-noack 运气好吗?
标签: java spring tomcat spring-security spring-boot