Tag Cloud
apple asterisk code flex iphone latex linux opensource planets privacy qt rant rock roller voip
apple asterisk code flex iphone latex linux opensource planets privacy qt rant rock roller voip
package my.own.security;
import java.security.Principal;
import java.util.List;
import javax.servlet.ServletConfig;
import org.acegisecurity.Authentication;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.providers.AuthenticationProvider;
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import flex.messaging.security.LoginCommand;
public class AcegiLoginCommand implements LoginCommand {
private static final Log log = LogFactory.getLog( AcegiLoginCommand.class );
protected static ApplicationContext applicationContext;
private static String[] CONFIG_LOCATIONS = {
"classpath:applicationContext.xml"
};
private static AuthenticationProvider authenticationProvider;
public AcegiLoginCommand() {
super();
if ( applicationContext == null ) {
log.debug( "Initializing Spring context");
try {
applicationContext = new ClassPathXmlApplicationContext(
getConfigLocations() );
} catch (Exception ex) {
log.error("exception loading spring context",ex);
}
log.debug( "... done !" );
}
if(authenticationProvider == null) {
authenticationProvider =
(AuthenticationProvider)applicationContext.getBean(
"myAuthenticationProviderSpringBean");
}
}
public Principal doAuthentication(String username, Object credentials) {
Authentication auth = authenticationProvider.authenticate(
new UsernamePasswordAuthenticationToken(username, credentials));
SecurityContextHolder.getContext().setAuthentication(auth);
return (Principal)auth;
}
public boolean doAuthorization(Principal arg0, List arg1) {
GrantedAuthority[] roles = ((UsernamePasswordAuthenticationToken)arg0).getAuthorities();
for(int n =0; n < roles.length; n++) {
for(int m = 0; m < arg1.size(); m++ ) {
if(roles[n].getAuthority().equals(arg1.get(m))) return true;
}
}
return false;
}
public boolean logout(Principal arg0) {
log.debug("logout called with arg: " + arg0);
// TODO
return false;
}
public void start(ServletConfig arg0) {
log.debug("start called with arg: " + arg0);
// TODO
}
public void stop() {
log.debug("stop called");
// TODO
}
private static String[] getConfigLocations() {
return CONFIG_LOCATIONS;
}
}
<login-command class="my.own.security.AcegiLoginCommand" server="Tomcat"/>
Hi!
I built a very similar thing for Granite DS (http://tech.groups.yahoo.com/group/graniteds/message/557)
Maybe we could share some ideas about ACEGI/Granite/FDS Integration...
Regards,
Luxspes
Posted by Luxspes on August 30, 2007 at 06:29 PM CEST #
Would you mind posting your acegi configuration xml?
Posted by Brian E on January 14, 2008 at 12:20 AM CET #
Thank you for this post, it was quite helpful. Just so others know, I had to change the following lines for my environment:
//The name of my Acegi configuration file.
private static String[] CONFIG_LOCATIONS =
{"classpath:security-context.xml"};
//"ldapAuthenticationProvider" is from my Acegi config file, and it's the name of the bean that is used for authentication via LDAP.
authenticationProvider =
(AuthenticationProvider)applicationContext.getBean("ldapAuthenticationProvider")\
;
I then updated services-config.xml and added:
<security>
<login-command class="com.gdais.security.AcegiLoginCommand"
server="Tomcat"/>
<security-constraint id="basic-read-access">
<auth-method>Basic</auth-method>
<roles>
<role>ROLE_MANAGERS</role>
<role>ROLE_USERS</role>
</roles>
</security-constraint>
</security>
//The roles came from the Acegi config file.
Posted by Geoffrey on July 15, 2008 at 08:14 PM CEST #