Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>thanks for the answers, but I finnally got it working in a totally programatic way.It wasn´t an easy solution, but I´d like to share It.</p> <p>First of all, I could split the problem into 2 parts:</p> <p>1) Create the proxy for my own beans.</p> <p>2) Add the security-roles to every bean. Unfortunately, i couldn´t add a xml-based pointcut, as the beans were all of the same class (a generic service). I wanted a way to secure each bean in a different way, despite they were all the same class. I decided to use the same declaration used by aopalliance ((execution <em>.</em> etc...)</p> <p>For whom it may interest, this is how I done:</p> <p>1) Used spring´s BeanNameAutoProxyCreator class, for creating the proxies around the classes i have manually added:</p> <pre><code>private void addProxies(ConfigurableListableBeanFactory beanFactory, List&lt;String&gt; exportedServices) { List&lt;String&gt; interceptorNames = findInterceptorNames(beanFactory); BeanNameAutoProxyCreator beanPostProcessor = new BeanNameAutoProxyCreator(); beanPostProcessor.setBeanNames(exportedServices.toArray(new String[exportedServices.size()])); beanPostProcessor.setInterceptorNames(interceptorNames.toArray(new String[interceptorNames.size()])); beanPostProcessor.setBeanFactory(beanFactory); beanPostProcessor.setOrder(Ordered.LOWEST_PRECEDENCE); beanFactory.addBeanPostProcessor(beanPostProcessor); } @SuppressWarnings("unchecked") private List&lt;String&gt; findInterceptorNames(ConfigurableListableBeanFactory beanFactory) { List&lt;String&gt; interceptorNames = new ArrayList&lt;String&gt;(); List&lt;? extends Advisor&gt; list = new BeanFactoryAdvisorRetrievalHelper(beanFactory).findAdvisorBeans(); for (Advisor ad : list) { Advice advice = ad.getAdvice(); if (advice instanceof MethodInterceptor) { Map&lt;String, ?&gt; beansOfType = beanFactory.getBeansOfType(advice.getClass()); for (String name : beansOfType.keySet()) { interceptorNames.add(name); } } } return interceptorNames; } </code></pre> <ul> <li>where exportedServices stands for the beans i wished to proxy</li> <li>The beanFactory instance could be fetched using my own implementation of BeanFactoryPostProcessor</li> </ul> <p>2) For the parameterizable secured-like part:</p> <p>Created a implementation of spring´s MethodDefinitionSource. implementors of this interface are used by MethodSecurityProxy to fetch securedObjects at runtime.</p> <pre><code> @SuppressWarnings("unchecked") @Override public ConfigAttributeDefinition getAttributes(Object object) throws IllegalArgumentException { if (!(object instanceof ReflectiveMethodInvocation)) { return null; } ReflectiveMethodInvocation invokation = (ReflectiveMethodInvocation) object; if (!(invokation.getThis() instanceof MyService)) { return null; } MyService service = (MyService) invokation.getThis(); Map&lt;String, String&gt; map =getProtectedServiceMethodMap(service); String roles = map.get(MethodKeyGenerator.generate(invokation.getMethod())); return roles == null ? null : new ConfigAttributeDefinition(StringUtils.delimitedListToStringArray(roles, ",")); } </code></pre> <ul> <li>Note that I had to put in a cache the methods wich I wanted to secure for each object instance.</li> </ul> <p>The trickiest part was to register my MethodDefinitionSource impl into the applicationContext, so that the MethodSecurityInterceptor became aware of it:</p> <pre><code> private void addCrudDefinitionSource() { DelegatingMethodDefinitionSource source = (DelegatingMethodDefinitionSource) beanFactory.getBean(BeanIds.DELEGATING_METHOD_DEFINITION_SOURCE); try { Field field = source.getClass().getDeclaredField("methodDefinitionSources"); field.setAccessible(true); List list = (List) field.get(source); list.add(new MyMethodDefinitionSource()); } catch (Exception e) { e.printStackTrace(); } } </code></pre> <p>Last, for checking Method entrypoints in a aop-style I used code like the following:</p> <pre><code> private void addToCache(){ // Ommiting parameters PointcutExpression expression = parser.parsePointcutExpression(fullExpression); Method[] methods = clazzToCheck.getMethods(); for (int i = 0; i &lt; methods.length; i++) { Method currentMethod = methods[i]; boolean matches = expression.matchesMethodExecution(currentMethod).alwaysMatches(); if (matches){ //Add to Cache } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload