views:

159

answers:

3

I want to run authentication/authorization only for the calls that come from HTTP requests.

The method on the controller I want to have authentication/authorization enabled is called from more than one source. In this case, it can be either called by another controller or by a direct HTTP request.

Can I turn off authentication/authorization for the calls that come from other Controllers?

Just read further if you haven't got this clear enough yet.

Let's say I have a method doIt() on a Controller A. I also have a Controller B, in which I inject controller A. At some point on Controller B, I call 'a.doIt()', but I can also call doIt() from an HTTP call to doIt.do. I want to test the call for authentication/authorization if the call comes from an HTTP call, but not if it comes from Controller B.

A: 

You need to only configure the spring authentication on URL and not on the method invocation. This will work for you.

Bhushan
This is a problem for me, as our current Voter uses annotations on the method to check for the required authorization level for that method. Changing this to URLs (probably with xml mappings) will probably be a lot of work. Although, if I can't find another solution, this will have to do the trick. Thanks!
Rodrigo Gama
If method level authorization is used then there is no way you can get away with that. URL authorization will use the same setup just some config you need to do, so not much of a work.
Bhushan
A: 

I don't see any way to do this, my guess is you'll just have to have a second method like doitDirectCall(..) that the actual other controller calls and doit(..) that get's called on an HTTP request.

Gandalf
+1  A: 

You are injecting in B the security proxied bean of A. Can't you inject A without the proxy?.
Bean A proxied:

<bean id="beanASecured" class="org.springframework.aop.framework.ProxyFactoryBean">
  <property name="targetName" value="beanA"/>
  <property name="interceptorNames">
    <value>securityInterceptor</value>
  </property>
</bean>

The secutiryInterceptor:

<bean id="securityInterceptor" class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor">
   ...
</bean>

Bean A not proxied:

<bean id="beanA" class="com.A"/>

Bean B injected with bean 'A not proxied':

<bean id="beanB" class="com.B">
   <constructor-arg ref="beanA"/>
</bean>
rodrigoap
Is there a way to do it? I don't think so. But this would be the best solution for me, actually.
Rodrigo Gama
It all depends on how are you proxing the beans. I edited my post to add an example.
rodrigoap
Happens that I inject them using the @Autowire annotation, along with the '<context:component-scan' tag. Is there a way to refer to the bean unintercepted using this method?
Rodrigo Gama
Yes, use de @Qualifier annotation.
rodrigoap
Just to make sure I got it right:I would have to declare two separate beans(annotated classes, in this case), one with security enabled and another with it disabled (possibly one inheriting from the other), Qualify each one with a different qualifier, and be explicit when autowiring, right?
Rodrigo Gama

related questions