views:

3417

answers:

1

I have a Spring application (Spring version 2.5.6.SEC01, Spring Security version 2.0.5) with the following setup (this is based off of this question):

In the security-config.xml file, I have the following configuration:

<http>
  <!-- Restrict URLs based on role -->
  <intercept-url pattern="/WEB-INF/jsp/login.jsp*" access="ROLE_ANONYMOUS" />
  <intercept-url pattern="/WEB-INF/jsp/header.jsp*" access="ROLE_ANONYMOUS" />
  <intercept-url pattern="/WEB-INF/jsp/footer.jsp*" access="ROLE_ANONYMOUS" />
  <intercept-url pattern="/login*" access="ROLE_ANONYMOUS" />
  <intercept-url pattern="/index.jsp" access="ROLE_ANONYMOUS" />
  <intercept-url pattern="/logoutSuccess*" access="ROLE_ANONYMOUS" />

  <intercept-url pattern="/css/**" filters="none" />
  <intercept-url pattern="/images/**" filters="none" />
  <intercept-url pattern="/**" access="ROLE_ANONYMOUS" />
  <anonymous />
  <form-login login-page="/login.jsp"/>
</http>

<beans:bean id="myUserDetailsService" class="com.example.login.MyUserDetailsService">
  <beans:property name="dataSource" ref="dataSource" />
  <custom-authentication-provider />
</beans:bean>

<authentication-provider user-service-ref="myUserDetailsService" />

The com.example.login.MyUserDetailsService class is defined:

public class MyUserDetailsService extends SimpleJdbcDaoSupport implements UserDetailsService {
  @Override
  public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException,
          DataAccessException {
    logger.info("MyUserDetailsService.loadUserByUsername: Entered method. Username [" + userName + "]");
    ...
  }
}

But I'm not seeing this log line. How do I define a custom UserDetailsService so I can set the security roles? I don't even need a custom service, but having this in the security-config.xml

<authentication-provider> <jdbc-user-service data-source-ref="dataSource" />
</authentication-provider>

wasn't setting the role even though I have the users and authorities tables. How can I set the Spring Security Roles?

+1  A: 

Just remove <custom-authentication-provider> element.

Your MyUserDetailsService IS NOT a custom AuthenticationProvider. Actually you are trying to supply the default DaoAuthenticationProvider with a custom UserDetailsService. Here is an example of working config for that scenario (and once again I recommend you to use auto-config):

<http auto-config = "true">
    <intercept-url pattern="/login.jsp" access="ROLE_ANONYMOUS" />
    ...
    <intercept-url pattern="/**" access="ROLE_USER" />

    <form-login login-page="/login.jsp" default-target-url="/XXX.html" />
</http>

<authentication-provider user-service-ref = "userDetailsService" />

<beans:bean id = "userDetailsService" class = "com.example.MyUserService" />

EDIT:

web.xml:

...
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
...

login.jsp:

...
<form method = "POST" action = "<c:url value = "/j_spring_security_check" />">
    <table>
        <tr>
            <td class = "label">Login:</td>
            <td><input type = "text" name = "j_username" /></td>
        </tr>
        <tr>
            <td class = "label">Password:</td>
            <td><input type = "password" name = "j_password" /></td>
        </tr>

        <tr>
            <td colspan = "2"><input type = "submit" value = "Log in" /></td>
        </tr>
    </table>
</form>
...
axtavt
This has the same result - I don't see anything in the MyUserService, whether the auto-config is true or not or if the <custom-authentication-provider> is present or not.
Tai Squared
The sample that I posted works fine for me. loadUserByUsername() is called when user clicks submit button in the login form.
axtavt
Is there anything else in your configuration or java class that is different? I have my security xml exactly as you have it above and the MyUserService is not being called.
Tai Squared
Looking at http://forum.springsource.org/showthread.php?t=65079 (post 6), it mentions that if a customer provider handles authentication, others won't be queried. I don't have a custom authentication provider (as I removed that tag, but it's not hitting my service. How is your login form configured in the servlet xml and jsp? Will that impact if my UserDetailsService is called or not?
Tai Squared
Have you checked your log? When <custom-authentication-provider /> is specifed on the UserService, an exeception appears in the log. If you missed it, you may miss some other problems with configuration.
axtavt