As title says, i'm developing a web application that receives user authentication infos from an external application. A spring controller of my app gets user info and stores it in session. I want to authenticate this user inside Spring Security and then use his roles to grant/deny access to urls like
<intercept-url pattern="/myprotectedpage*" access="hasRole('rightrole')" />
I read some tutorials speaking about PRE_AUTH_FILTER and UserDetailsService but i can't get the point. What is the application lifecycle of Spring Security? Which classes are involved? I need some full working samples.
implement a service that holds the user information.
your controllers populates the UserAuthenticationInfoService service with the user information which you receive from your external application.
then implement a custom UserDetailsService to acces these information.
and setup spring security context to use this UserDetailsService (you'll find it in the spring security documentation)
You can implement your own Custom AuthenticationManager and Custom UsernamePasswordAuthenticationFilter. This is simple example but it can give you an idea also for your information this is very sensitive part of security context:)
Simply create beans in your spring_security.xml:
When you implement CustomUsernamePasswordAuthenticationFilter override Authentication and add your external logic:
Then generated authentication object will be handled by authentication manager:
Then if required you can override default authentication object too,if roles dynamically located here is where you handle:
There are lots of tuts out there for the same, just need to google properly.
Anyway the best i have found till date (for almost all spring tuts) is Krams and here's the one for basic spring security.
http://krams915.blogspot.com/2010/12/spring-security-mvc-integration_18.html
For Implementing UserDetailService here's the link
http://krams915.blogspot.in/2012/01/spring-security-31-implement_5023.html
Some others are :
EDIT
This is how my own application does the authentication (Please note that i dont use external authentication, I simpply get details from DB but i guess it should not be much of an issue).
My
security-context.xml
:Now as you see i have specified a bean named
loginService
as my authentication provider which is a bean for classcom.indyaah.service.LoginService
.The code for the same is : Pl Note I have truncated unnecessary code
Note 2 things over here.
org.springframework.security.core.userdetails.User
object which is then returned by the method to spring security.