annotation for acegi
개발/Java2010. 5. 27. 18:25
UPDATE: The new version of acegi has itself’s annotation implement. So you don’t need this any more.
annotation is so good that i can’t wait to use it in acegi
code sample
import java.util.List;here’s my implementation for it
import org.gotblog.common.acegi.SecurityConfig;
import org.springframework.transaction.annotation.Transactional;
public interface BookManager {
@Transactional
@SecurityConfig( { "ROLE_USER", "ROLE_ANONYMOUS" })
public List listBook();
}
package org.gotblog.common.acegi;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Target( { TYPE, METHOD })
@Retention(RUNTIME)
public @interface SecurityConfig {
String[] value();
}
package org.gotblog.common.acegi;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.springframework.metadata.Attributes;
public class AcegiAnnotationAttributes implements Attributes {
// TODO need to add cache
public Collection getAttributes(Class targetClass) {
Set<net.sf.acegisecurity.SecurityConfig> configs =
new HashSet<net.sf.acegisecurity.SecurityConfig>();
for (Annotation annotation : targetClass.getAnnotations()) {
if (annotation instanceof SecurityConfig) {
SecurityConfig config = (SecurityConfig) annotation;
for (String value : config.value()) {
configs.add(
new net.sf.acegisecurity.
SecurityConfig(value));
}
break;
}
}
return configs;
}
public Collection getAttributes(Class targetClass, Class filter) {
throw new IllegalArgumentException("Not support filter");
}
public Collection getAttributes(Method targetMethod) {
Set<net.sf.acegisecurity.SecurityConfig> configs =
new HashSet<net.sf.acegisecurity.SecurityConfig>();
for (Annotation annotation : targetMethod.getAnnotations()) {
if (annotation instanceof SecurityConfig) {
SecurityConfig config = (SecurityConfig) annotation;
for (String value : config.value()) {
configs.add(
new net.sf.acegisecurity.
SecurityConfig(value));
}
break;
}
}
return configs;
}
public Collection getAttributes(Method targetMethod, Class filter) {
throw new IllegalArgumentException("Not support filter");
}
public Collection getAttributes(Field targetField) {
throw new IllegalArgumentException("Not support field annotation");
}
public Collection getAttributes(Field targetField, Class filter) {
throw new IllegalArgumentException("Not support field annotation");
}
}
<bean id="acegiAnnotationAttributes"
class="org.gotblog.common.acegi.AcegiAnnotationAttributes"/>
<bean id="objectDefinitionSource"
class="net.sf.acegisecurity.intercept.method.MethodDefinitionAttributes">
<property name="attributes">
<ref local="acegiAnnotationAttributes"/>
</property> </bean>
it works fine and i haven’t optimized it for performance
출처 : http://weavesky.com/2005/04/19/annotation-for-acegi/
'개발 > Java' 카테고리의 다른 글
JAVA를 이용한 정규식 간단 사용법 (0) | 2010.09.30 |
---|---|
Weblogic에러 - Didn't meet stated Content-Length (0) | 2010.05.28 |
Spirng MVC 와 Tiles 2 연동하기(응용) (0) | 2010.05.26 |
Spirng MVC 와 Tiles 2 연동하기(기본) (0) | 2010.05.26 |
Spring MVC Annotation기반으로 사용시 URL기반 요청 문제점 해결방법 (0) | 2010.05.25 |