Guess what this blog is about Sherlock! :) - Jeryl Cook

Wednesday, October 20, 2010

Restlet ,Guard, Spring Security 3.0, and HTTP BASIC AUTHENTICATION o my!

Not sure what happen to this guy eshepelyuk and the project
restlet-spring-security

But I needed a clean way to integrate Spring Security 3.0.x into a Restlet Application I've been working on..

I updated the code(ServiceSpringSecurityGuard.java)
to work with Spring 3.0.x so here it is:

package org.restlet.ext.spring.security;

import org.restlet.Guard;
import org.restlet.data.ChallengeScheme;
import org.restlet.data.Request;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.config.BeanIds;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.Assert;

public class ServiceSpringSecurityGuard
extends Guard  implements ApplicationContextAware, InitializingBean {

 private AuthenticationManager authentificationManager;
 private ApplicationContext applicationContext;

 public ServiceSpringSecurityGuard() {
  super(null, ChallengeScheme.HTTP_BASIC, "Spring Security");
 }

 public AuthenticationManager getAuthentificationManager() {
  return authentificationManager;
 }

 public void setAuthentificationManager(AuthenticationManager authentificationManager) {
  this.authentificationManager = authentificationManager;
 }

// private AccessDecisionManager accessDecisionManager;

// public AccessDecisionManager getAccessDecisionManager() {
//  return accessDecisionManager;
// }
//
// public void setAccessDecisionManager(AccessDecisionManager accessDecisionManager) {
//  this.accessDecisionManager = accessDecisionManager;
// }

 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  this.applicationContext = applicationContext;
 }

 public void afterPropertiesSet() throws Exception {
  Assert.notNull(this.applicationContext, "applicationContext is null");

//  if (null == accessDecisionManager) {
//   setAccessDecisionManager((AccessDecisionManager) applicationContext.getBean(BeanIds.ACCESS_MANAGER));
//  }

  if (null == authentificationManager) {
   setAuthentificationManager((AuthenticationManager) applicationContext.getBean(BeanIds.AUTHENTICATION_MANAGER));
  }

  Assert.notNull(this.authentificationManager, "authentificationManager should be specified");
//  Assert.notNull(this.accessDecisionManager, "accessDecisionManager should be specified");
 }

 @SuppressWarnings("unused")
 public boolean checkSecret(Request request, String identifier, char[] secret) {
  try {
   Authentication auth = authentificationManager.authenticate(new UsernamePasswordAuthenticationToken(identifier, new String(secret)));
   if (auth.isAuthenticated()) {
    SecurityContextHolder.getContext().setAuthentication(auth);
   }
   return auth.isAuthenticated();
  } catch (AuthenticationException e) {
   SecurityContextHolder.getContext().setAuthentication(null);
   return false;
  }
 }
}

Labels: , , ,

Wednesday, October 13, 2010

Cassandra-0.7.0 and Hector.

*sigh*...midnight ...and I have tons of coding tomorrow at 6am!

Anyway hope this helps someone..

When connecting to Apache-cassandra-0.7.0-beta2 use Hector-0.6.0.17 and not the other connection crap using sockets,etc(in the test cases,sources and samples.).....it will hang when trying to getKeyspace.

sooo,its something like this...

CassandraClientPool pool = CassandraClientPoolFactory.INSTANCE.get(); 
        CassandraClient client = pool.borrowClient(localhost, 9160);        
        Cassandra.Iface  cassandraClient = client.getCassandra();


Just to note this only work when using Hector to connection to Cassandra...
I am working on a project that requires 'real-time' indexing/searching so "Lucandra" was my obvious choice!
as of right now Lucandra-0.7 works ONLY with

Apache-cassandra-0.7.0-beta1 and NOT beta2... Also stop the 'connection' pool insanity and trust me, you want to modify the source to use Hector-0.6.0.17 just remember you have to set the keyspace after you create a Cassandra client.

cassandraClient.set_keyspace("Lucandra");

Labels: , ,