2
스프링 레벨 보안을 사용하여 메소드 레벨에서 액세스 규칙을 정의하고 있으며, 스프링 보안 주석이 서비스 레이어에서 작동하지 않습니다. 그러나 컨트롤러 레이어에서는 정상적으로 작동합니다.스프링 보안 주석이 서비스 레이어에서 작동하지 않습니다.
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder());
}
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/api/register")
.antMatchers("/api/activate")
.antMatchers("/api/lostpassword")
.antMatchers("/api/resetpassword");
}
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true)
private static class GlobalSecurityConfiguration extends GlobalMethodSecurityConfiguration {
@Autowired
private MutableAclService mutableAclService;
@Autowired
private RoleHierarchy roleHierarchy;
public GlobalSecurityConfiguration() {
super();
}
@Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
DefaultMethodSecurityExpressionHandler expressionHandler = new DefaultMethodSecurityExpressionHandler();
expressionHandler.setPermissionEvaluator(new AclPermissionEvaluator(mutableAclService));
expressionHandler.setRoleHierarchy(roleHierarchy);
return expressionHandler;
}
}
}
이서비스가 작동하지 않습니다 :
@Override
@PreAuthorize("hasRole('ROLE_ADMIN')")
public Iterable<Appliance> getAll() {
return applianceRepo.findAll();
}
컨트롤러가 잘 작동 : 여기
내 구성입니다@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<PagedResources<Appliance>> getPage(@PageableDefault Pageable pageable, PagedResourcesAssembler pagedAssembler) {
Page<Appliance> appliancePage = applianceService.getPage(pageable);
return ResponseEntity.ok(pagedAssembler.toResource(appliancePage, applianceAssembler));
}
WebSecurityConfig 또는 @ 어딘가에 @EnableGlobalMethodSecurity 주석을 추가해야합니다. – Zemzela
@Zemzela @EnableGlobalMethodSecurity (prePostEnabled = true, jsr250Enabled = true)를 추가했지만 작동하지 않습니다. –
다음을 읽어보십시오. http://websystique.com/spring-security/spring-security-4-method-security-using-preauthorize-postauthorize-secured-el 도움이 될 것입니다. – Zemzela