I'm trying to create REST API and web/MVC application in Spring. They both should use the same service layer. Can I somehow use two completely different configurations in Spring (Token authentication for API, cookies for web, 404 page for web, etc)? Or should I make two independent Spring applications?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Spring-MVC
andSpring-Security
Spring-MVC configuration by default facilitates
Controller can return ModelAndView for Web application view serving purpose.
Controller can be used as
RestController
where response is by default processed byHttpMessageConverters
where controller methods used asRest-API
However we can use
Spring-Security
which is a filter based framework and it acts as asecurity-wall(http-firewall) between your Rest-APIs and client-app consuming Rest API
Or
security-wall(http-firewall) between
Spring-MVC
application and end-userIf requirement is
then Implementation considerations
Implementation-type 1. Rest APIs should only accessed if auth token is present and valid.
Implementation-type 2. Rest APIs can be accessed by auth token as well as session.
Implementation-type 1
"/api/**"
rest of url's will not be considered by this configuration. This http configuration will be configured for stateless. And you should configure an implementation ofOncePerRequestFilter
(SayJwtAuthFilter
) and filter order can be beforeUsernamePasswordAuthenticationFilter
orBasicAuthenticationFilter
. But your filter should read the header for auth token, validate it and should createAuthentication
object and set it toSecurityContext
without fail.JwtAuthFilter
but configuresUsernamePasswordAuthenticationFilter
(.formLogin()
does this for you)Implementation-type 2
"/**"
UsernamePasswordAuthenticationFilter
andJwtAuthFilter
butJwtAuthFilter
should be configured beforeUsernamePasswordAuthenticationFilter
.UsernamePasswordAuthenticationFilter
and attemptAuthentication method ofUsernamePasswordAuthenticationFilter
will get invoked if there is no valid auth object inSecurityContext
. IfJwtAuthFilter
validates token and sets auth object toSecurityContext
then even if filter chain reachesUsernamePasswordAuthenticationFilter
attemptAuthentication method will not be invoked as there is already an authentication object set inSecurityContext
.This is all about both type of implementation, you can go for any type of implementation depending upon your requirement. And for both implementation type
JwtAuthenticationTokenFilter
andJwtTokenUtil
is common and is given below.JwtAuthenticationTokenFilter
JwtTokenUtil
You can download working example from my github repository link given below.
Implementation type-1
Implementation type-2
If you are curious about sequence of execution in Spring Security you can refer my answer here -> How spring security filter chain works
You can write a rest controller and normal controller for all endpoints. Spring security will automatically add an auth flow when you add it, and if you want to override you can do that in the configuration.
Rest Controller for
/api/foo
Normal controller for
/ui/foo
This way you can separate cookie logic and manage redirects and validations, in the normal controller.