Reputation: 13
I am working on an application that uses Spring MVC and Spring REST, it is separated into 2 servers, the backend is a Spring REST interface and the frontend is a normal SPRING MVC application. I however have some security requirements especially for the MVC server frontend eg
My question are
Any pointers would be very appreciated.
Upvotes: 1
Views: 520
Reputation: 17487
1) Is there a way to implement a custom login module that will authenticate users but still use Spring Security to manage access control after login and for logout.
Yes. You can provide your own authentication mechanism, by implementing org.springframework.security.authentication.AuthenticationProvider and making it a bean (annotating it or in XML) :
@Service("myAuthenticationProvider") public class TangoAuthenticationProvider implements AuthenticationProvider{
@Override public boolean supports(Class<?> authentication) { //your code } @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { //your code }
Then , and instruct Spring security to use it (in your security context):
<authentication-manager>
<authentication-provider ref="tangoAuthenticationProvider" />
</authentication-manager>
See this question, and of course spring security doc.
2) Dynamically created roles : I can't answer thios part, no experience with this.
3) Single session requirement
Maybe there is sucha a mechanism built in Spring Security (you' d have to research this), but I think you can implement it using simple session listeners and the afore-mentionned custom authentication mechanism :
Create a service that allows to store a reference to a session associated with its id, and provide access to the session by its id. You could use a static hashmap, or a singleton, or better, a Spring service bean with roughly the following interface (let's call it the session repository):
public void putSession(String id, HttpSession session); public HttpSession getSessionById(String id);
In your authentication provider, after a successful login, set the user's session id field to the current session id
This is sensitive code in relation to security (cross-session stuff) so it should be written and tested very carefully though !
I hope it helps.
Upvotes: 1
Reputation: 114
you can use a form to login.
in order to create dynamic role, you could implement the UserDetailsService interface with one method,
public UserDetails loadUserByUsername(String userId)
throws UsernameNotFoundException, DataAccessException
{
...
/*
fetch your role information every time the user re-login
you can store the new role in the database and fetch it from here
*/
}
after that, you can configure your spring-security file on authentication/authorization with database role
For the single seesion per user requirement, try to keep all the user - sessionid pair in a database or in an application level hashmap. there is a spring ContextLoaderListener,( HttpSessionListener under the hook) you can therefore add/remove user-sessionid pair from the application from the listener.
Upvotes: 0