views:

4109

answers:

3

Hi, I am new to spring and spring security,

I have understood how beans are created and referenced in the xml files, I need to provide security using spring into my application.

I included a custom applicationContext-security.xml file in my web.xml : contextConfigLocation

in this file, I have intercepted url patterns using

<intercept-url pattern='/**.something' access="IS_AUTHENTICATED_FULLY"/>

inside element.

I have set the form for login as now, if a page is not authorised it shows me my custom Login.html page.

Now for the issues I am facing:

  1. How do I specify my login form to pass its value to spring ?
  2. How do I use my own authentication-provider ?

I tried this:

<authentication-provider user-service-ref="userDetailsService"/>
<beans:bean id = "userDetailsService" class ="com.somepath.CustomAuthenticationProvider">
     <custom-authentication-provider/>
    </beans:bean>

where CustomAuthenticationProvider implements AuthenticationProvider

but the code throws an error: Error creating bean with name '_filterChainProxy' .... No UserDetailsService registered

Please help

+1  A: 

I am somewhat new to Spring myself but I will try to help you. The intercept-url looks fine.

I don't think the authentication-provider is right. Take a look at my code:

  <beans:bean id="MyUserDetailsService" class="path.to.MyAuthenticationService"/>

<beans:bean id="userDetailsService" class="org.springframework.security.userdetails.hierarchicalroles.UserDetailsServiceWrapper" >
    <beans:property name="roleHierarchy" ref="roleHierarchy" />
    <beans:property name="userDetailsService">
      <beans:ref bean="MyUserDetailsService"/>
    </beans:property>
  </beans:bean>
 <authentication-provider user-service-ref="userDetailsService">
   <password-encoder hash="md5"/>
 </authentication-provider>

You may not need the role heirarchy.

You have a login form on a jsp page. The form should begin something like this:

<form:form modelAttribute="login">

Also you must map the appropriate fields.

<form:input path="login">
<form:password path="password">

in your applicationContext-security.xml set the login page:

<form-login login-page="/login.jsp" default-target-url="/login.html" always-use-default-target="true" authentication-failure-url="/login.jsp?login_error=1"/>

login.html should be mapped to your LoginController.java which extends BaseController and implements a login method which takes at least a HttpServletRequest and Model as parameters. Mine then works by calling the following Spring class/methods:

String userlogin = SecurityContextHolder.getContext().getAuthentication().getName();

If your CustomAuthenticationProvider is implemented correctly you can then (hopefully) get the user's details from your Model and finally:

return "redirect:homepage.html";

I may have missed something if you're still having trouble let me know in a comment.

RobbR
I succeeded in sending the parameters accross using fields "j_username" and "j_password" in my request.Now, at the server end, i require to authenticate these manually rather than ask spring to do sothis is what i attempted:
Salvin Francis
class CustomAuthenticationProvider implements AuthenticationProvider{...public boolean supports(Class authentication) { return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication)); }...public Authentication authenticate(Authentication authentication) throws AuthenticationException {//This is where i am confused.//What should this method return to an unsucessful attempt??//i tried 1. null and 2. authentication.setAuthenticated(false);return authentication;}}
Salvin Francis
<beans:bean id = "userDetailsService" class ="com.somePath.auth.CustomAuthenticationProvider"> <custom-authentication-provider/> </beans:bean>
Salvin Francis
+4  A: 

1: How do I specify my login form to pass its value to spring ?

After you setup your standard Spring Filter in web.xml for Spring Security, using some of the default settings configured by the <http> tag. An instance of AuthenticationProcessingFilter is created for you as part of the chain of filters.

My default the AuthenticationProcessingFilter is set up to read j_username and j_password as the username / password token.

In order to override this, replace your customize AuthenticationProcessingFilter over the default one by doing this:

<bean id=“myAuthFilter” class=“org.springframework.security.ui.webapp.AuthenticationProcessingFilter” >
<security:custom-filter position=“AUTHENTICATION_PROCESSING_FILTER”/><!–-replace the default one-–>
  <property name=“usernameParameter” value=“myUsername”/><!-- myUsername is the name of the input tag where user enter their username on the HTML page -->
  <property name=“passwordParameter” value=“myPassword” /><!–- myPassword is the name of the input tag where user enter their password on the HTML page -–>
</bean>

See also the JavaDoc of AuthenticationProcessingFilter for more details: http://static.springsource.org/spring-security/site/apidocs/org/springframework/security/ui/webapp/AuthenticationProcessingFilter.html

2: How do I use my own authentication-provider?

Using the following code:

<bean id="myAuthenticationProvider" class="com.something.MyAuthenticationProvider">
    <security:custom-authentication-provider />
</bean>

<security:custom-authentication-provider /> is the tag that let's spring knows this is a custom provider and the Authentication Manager should use it in its provider chain.

Source: http://static.springsource.org/spring-security/site/docs/2.0.x/reference/appendix-namespace.html#d4e3379

3: Regarding the issue with the code throwing '_filterChainProxy' .... No UserDetailsService registered...'

Is com.somepath.CustomAuthenticationProvider implementing the UserDetailService interface?

lsiu
I resolved "3" i am stuck up on "2.How do I use my own authentication-provider?"as mentioned in my above comment to RobbR, I am not able to indicate user that authorisation failed.Is my class incorrect?
Salvin Francis
Your CustomAuthenticationProvider needs to implement UserDetailService in order for the "default" configuration to work. Otherwise, there is some extra configuration you need to do. I don't have it handy right now.
lsiu
Looks like you may need to disable remember-me? Are you using auto-config? It will help if you can you full config file.Remove the '<authentication-provider user-service-ref="userDetailsService"/>' tag if you provider is not implementing the UserDetailsService.
lsiu