All you got to do in Micronaut is to go through the steps described in the Micronaut Security Guide.
TL;DR
The Spring Boot feature you're mentioning is not available in Micronaut. But the following easy steps will provide to get there as close as possible.
Mainly that's following these steps:
- Add these dependencies to your
build.gradle
annotationProcessor("io.micronaut.security:micronaut-security-annotations")
implementation("io.micronaut.security:micronaut-security-jwt")
- Then the security is enabled by default but you can adjust this by modifying your
application.yml
micronaut:
security:
enabled: true # true is the default value
You can either secure your controller by using the @Secured annotation or define intercepted urls in your application.yml
micronaut:
security:
basic-auth:
enabled: true # enabled basic authentication
intercept-url-map:
- pattern: /**
access:
- isAuthenticated()
Then you write a simple authentication provider that does the login verifcation.
import io.micronaut.context.annotation.Requires;
import io.micronaut.http.HttpRequest;
import io.micronaut.security.authentication.AuthenticationProvider;
import io.micronaut.security.authentication.AuthenticationRequest;
import io.micronaut.security.authentication.AuthenticationResponse;
import jakarta.inject.Singleton;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
@Singleton
public class AuthenticationProviderUserPassword implements AuthenticationProvider {
@Override
public Publisher<AuthenticationResponse> authenticate(HttpRequest<?> httpRequest, AuthenticationRequest<?, ?> authenticationRequest) {
return Mono.<AuthenticationResponse>create(emitter -> {
if (authenticationRequest.getIdentity().equals(System.getenv("YOUR-USER")) && authenticationRequest.getSecret().equals(System.getenv("YOUR-PWD"))) {
emitter.success(AuthenticationResponse.success("user"));
} else {
emitter.error(AuthenticationResponse.exception());
}
});
}
}
That does the job for a good start.