I followed the code from http://static.springsource.org/sprin...ted-principals and here's what I got from my test program.
Code:
[DEBUG] [http-8080-Processor22 05:27:14] (MainController.java:getCommonPage:32) Received request to show common page
[DEBUG] [http-8080-Processor22 05:27:14] (MainController.java:getCommonPage:37) Total logged-in users: 2
[DEBUG] [http-8080-Processor22 05:27:14] (MainController.java:getCommonPage:38) List of logged-in users:
[DEBUG] [http-8080-Processor22 05:27:14] (MainController.java:getCommonPage:40) jane
[DEBUG] [http-8080-Processor22 05:27:14] (MainController.java:getCommonPage:40) john
I'm able to retrieve the total number of logged-in users and also their names.
I suggest you setup a working example that uses a custom authentication manager like the tutorial I provided at http://krams915.blogspot.com/2010/12...-using_26.html Then follow the reference at http://static.springsource.org/sprin...ted-principals
Then, for example, in your controller, inject an instance of SessionRegistryImpl
Code:
@Resource(name="sessionRegistry")
private SessionRegistryImpl sessionRegistry;
Then just use a logger to test the output:
Code:
logger.debug("Total logged-in users: " + sessionRegistry.getAllPrincipals().size());
logger.debug("List of logged-in users: ");
for (Object username: sessionRegistry.getAllPrincipals()) {
logger.debug(username);
}
There's a huge footnote at the bottom of the Spring Security Reference:
Authentication by mechanisms which perform a redirect after authenticating (such as form-login) will not be detected by SessionManagementFilter, as the filter will not be invoked during the authenticating request. Session-management functionality has to be handled separately in these cases.
That's why you're advised to add a FORM_LOGIN_FILTER
Code:
<security:custom-filter ref="authenticationFilter" position="FORM_LOGIN_FILTER"/>
I don't think you really need to add a custom authentication manager. Just make sure you put the filters correctly.
Here's my spring-security.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<!-- This is where we configure Spring-Security -->
<security:http auto-config="false" use-expressions="true" access-denied-page="/krams/auth/denied"
entry-point-ref="authenticationEntryPoint" >
<security:intercept-url pattern="/krams/auth/login" access="permitAll"/>
<security:intercept-url pattern="/krams/main/admin" access="hasRole('ROLE_ADMIN')"/>
<security:intercept-url pattern="/krams/main/common" access="hasRole('ROLE_USER')"/>
<security:logout
invalidate-session="true"
logout-success-url="/krams/auth/login"
logout-url="/krams/auth/logout"/>
<security:custom-filter ref="blacklistFilter" before="FILTER_SECURITY_INTERCEPTOR"/>
<security:custom-filter ref="authenticationFilter" position="FORM_LOGIN_FILTER"/>
<security:custom-filter position="CONCURRENT_SESSION_FILTER" ref="concurrencyFilter" />
<security:session-management session-authentication-strategy-ref="sas"/>
</security:http>
<!-- Custom filter to deny unwanted users even though registered -->
<bean id="blacklistFilter" class="org.krams.tutorial.filter.BlacklistFilter" />
<!-- Custom filter for username and password. The real customization is done in the customAthenticationManager -->
<bean id="authenticationFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"
p:authenticationManager-ref="customAuthenticationManager"
p:authenticationFailureHandler-ref="customAuthenticationFailureHandler"
p:authenticationSuccessHandler-ref="customAuthenticationSuccessHandler"
p:sessionAuthenticationStrategy-ref="sas"/>
<!-- Custom authentication manager. In order to authenticate, username and password must not be the same -->
<bean id="customAuthenticationManager" class="org.krams.tutorial.manager.CustomAuthenticationManager" />
<!-- We just actually need to set the default failure url here -->
<bean id="customAuthenticationFailureHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"
p:defaultFailureUrl="/krams/auth/login?error=true" />
<!-- We just actually need to set the default target url here -->
<bean id="customAuthenticationSuccessHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler"
p:defaultTargetUrl="/krams/main/common" />
<!-- The AuthenticationEntryPoint is responsible for redirecting the user to a particular page, like a login page,
whenever the server sends back a response requiring authentication -->
<!-- See Spring-Security Reference 5.4.1 for more info -->
<bean id="authenticationEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint"
p:loginFormUrl="/krams/auth/login"/>
<!-- The tag below has no use but Spring Security needs it to autowire the parent property of
org.springframework.security.authentication.ProviderManager. Otherwise we get an error
A probable bug. This is still under investigation-->
<security:authentication-manager/>
<bean id="concurrencyFilter"
class="org.springframework.security.web.session.ConcurrentSessionFilter">
<property name="sessionRegistry" ref="sessionRegistry" />
<property name="expiredUrl" value="/session-expired.htm" />
</bean>
<bean id="sas" class=
"org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy">
<constructor-arg name="sessionRegistry" ref="sessionRegistry" />
<property name="maximumSessions" value="1" />
</bean>
<bean id="sessionRegistry" class="org.springframework.security.core.session.SessionRegistryImpl" />
</beans>