I wanted to control access with Spring Security method annotations.
Annotated an existing class.
MyService.java
@Component
public class MyService {
@PreAuthorize("hasRole('ROLE_ADMIN')")
public void someAdminAction() {
/* ... */
}
}
I created a configuration class to enable annotations.
MethodSecurityConfiguration.java
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfiguration {
}
MyService
was injecting into the controller.
MyController.java
@RestController
public class MyController {
@Autowired
private MyService myService;
/* ... */
}
I can't inject MyService into MyController!
Something called Type Mismatch! !!
The annotated class AOPs and becomes a proxy, so it seems that the type has changed (?).
Added the option proxyTargetClass = true
.
MethodSecurityConfiguration.java
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
public class MethodSecurityConfiguration {
}
Recommended Posts