[Java] [Spring] What to do if you cannot Autowire with Type Mismatch after annotating Spring Security

background

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;

    /* ... */
}

What happened

I can't inject MyService into MyController!

Something called Type Mismatch! !!

Cause

The annotated class AOPs and becomes a proxy, so it seems that the type has changed (?).

solution

Added the option proxyTargetClass = true.

MethodSecurityConfiguration.java


@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
public class MethodSecurityConfiguration {

}

Recommended Posts

[Java] [Spring] What to do if you cannot Autowire with Type Mismatch after annotating Spring Security
What to do if you cannot execute with the command "Java package name / class name"
What to do if you get angry with OpenSSL with pyenv install
What to do if you cannot roll back the migration (UnknownMigrationVersionError)
[Rails] What to do if you can't get parameters with form_with
What to do if you install Ubuntu
What to do if JavaMail cannot be used with OpenJDK 11 or later
[Composer] [Laravel] What to do if you cannot install due to insufficient memory
What to do if you push incorrect information
What to do if you have installed Java for OS X on macOS
What to do if you accidentally create a model
What to do if you get a NoClassDefFoundError when trying to run eclipse on Java9
What to do if you can't find your API key after deploying to Rails heroku
What to do if you get a java.io.IOException in GlassFish
What to do when you launch an application with rails
What to do if you change the Listen Address from the settings screen with Mattermost docker
What to do if you installed Ruby with rbenv but the version does not change
# What to do if you accidentally do rails db: migrate: drop
What to do if you get a "Cannot Pull Container Error" when starting ECS ​​Fargate
[Rails] What to do if you can't get an error message with the errors method
What to do if the adb command cannot be executed
What to do if you can't use the rails command
What to do if you get "Changes not staged for commit:" when you git status after git add.
What to do if you get a gcc error in Docker
What to do if validation doesn't work with the update action
What to do if you get a DISPLAY error in gym.render ()
What to do if you forget the root password in CentOS7
What to do after Vagrant install
What to do if you get an error with bundle install by entering gem'bcrypt' in your Gemfile
What to do if you get Application with name appName is already registered. When you try to start GlassFish
What to do if you should have installed Rails but an error occurs with rails -v (for beginners)
What to do if you can't bundle update and bundle install after installing Ruby 3.0.0 in the Rails tutorial
What to do if you get the error Too long with no output (exceeded 10m0s) on CircleCI
What to do if changes are not reflected after automatic deployment to EC2
What to do if you get a groovy warning in Thymeleaf Layout
What to do if you get an error during rails db: reset
What to do if the background image is not applied after deployment
What to do when CentOS cannot be started with VirtualBox on Catalina
What to do if you get an uninitialized constant Likes Controller error
What to do if you can't install the plugin from the Eclipse marketplace
What to do if you don't like the code generated by swagger-codegen-cli
What to do if you get an error when you hit Heroku logs
What to do if you get a MiniMagick vulnerability alert on GitHub
What to do if the build fails with the error "Module compiled with Swift x.x.x cannot be imported by the Swift x.x.x compiler"
If you dare to compare Integer with "==" ...
What to do after updating Dockerfile, docker-compose.yml
What to do if you have enabled Use the WSL2 based engine in Docker Desktop with insufficient WSL2 installation
What to do if you get Could not save master table to file after importing a project in Eclipse
How to translate the error message into Japanese (What to do if you cannot log in for some reason)
What to do if you get a SQLite3 :: BusyException: database is locked error
What to do when you think you can't do Groovy-> Java in IntelliJ IDEA CE
What to do if you get Could not locate Gemfile or .bundle / directory
What to do if you can't activate the select box created by bootstrap-select
What to do if you get the error message unrecognized selector send to instance "***"
[Java] Why do you bother to use the interface (Spring is also available)
wildflly10 java8 ERROR [org.jboss.jca.core.tx.jbossts.XAResourceRecoveryImpl] (Periodic Recovery) IJ000906 What to do if an error occurs
What to do if you get an error on heroku rake db: migrate
What to do if you get angry if you don't have nokogiri while installing wp2txt
What to do if you get To install the missing version, run `gem install bundler: 2.1.4`
What to do if SPA URL loading cannot be detected by WebView shouldOverrideUrlLoading
WELD-001408: What to do when Unsatisfied dependencies for type Bean with qualifiers @Default