We have an application which uses spring basic authentication for security. The application is not built on Spring MVC. We are using the polymer as front end and exposing services as rest based one.
The requirement is to implement a login form, on click of submit button need to invoke a javascript which should post username/password.
Is there a way to pass credentials to spring basic authentication servlet from javascript, so that it validates the request. We have implemented AuthenticationProvider authenticate to perform the validation.
Authentication java code
import org.springframework.security.authentication.*;
import org.springframework.security.core.*;
import java.util.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import com.lifesciencemeta.ls.User;
import com.lifesciencemeta.ls.LifeScienceServiceMaster;
import com.lifesciencemeta.ls.LifeScienceConstants;
public class SpringBasicAuthentication implements AuthenticationProvider {
public LifeScienceServiceMaster lifeScienceService;
@Context
UriInfo lsUri;
public LifeScienceServiceMaster getLifeScienceService() {
return lifeScienceService;
}
public void setLifeScienceService(LifeScienceServiceMaster lifeScienceService) {
this.lifeScienceService = lifeScienceService;
}
@Override
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
UsernamePasswordAuthenticationToken auth = (UsernamePasswordAuthenticationToken) authentication;
String principal = (String) auth.getPrincipal();
String credential = (String) auth.getCredentials();
User u = lifeScienceService.authenticateUser(principal, credential);
if (u == null)
throw new BadCredentialsException(LifeScienceConstants.getMsg(“Auth Failed"));
else {
List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>();
String role = u.getRole().getName();
if(role == null) {
throw new BadCredentialsException(LifeScienceConstants.getMsg(“Auth Failed"));
}
grantedAuths.add(new SimpleGrantedAuthority(role));
UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(
principal, credential, grantedAuths);
result.setDetails(u);
return result;
}
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
Web.xml
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> org.springframework.web.context.ContextLoaderListener contextConfigLocation /WEB-INF/context.xml springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxy springSecurityFilterChain / Jersey REST Service com.sun.jersey.spi.container.servlet.ServletContainer com.sun.jersey.config.property.packages com.lifesciencemeta.ls.restService.Invoke com.sun.jersey.api.json.POJOMappingFeature true 1 Jersey REST Service /rest/
Security.xml
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<security:global-method-security secured-annotations="enabled" jsr250-annotations="enabled" pre-post-annotations="enabled" />
<security:http>
<security:intercept-url pattern="/**" access="ROLE_USER, ROLE_ADMIN"/>
<security:http-basic />
</security:http>
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider ref="SpringBasicAuthentication" />
</security:authentication-manager>
<bean id="SpringBasicAuthentication"
class="com.lifesciencemeta.ls.SpringBasicAuthentication" >
<property name="lifeScienceService" ref="lsLifeScienceServiceImpl"/>
</bean>
</beans>