Authorize all controllers in a given namespace? - fluent-security

Is it possible to apply an AuthorizeAttribute to all controllers within a given namespace using FluentSecurity?

Yes, you can do that using ForAllControllersInNamespaceContainingType<TType>() or ForActionsMatching(info => *predicate*) in FluentSecurity 2.0 (currently in beta).
You can read more about it in the documentation here: https://github.com/kristofferahl/FluentSecurity/wiki/Securing-controllers

Related

java : Is it possible to dynamically modify the code of a servlet's method (like doPost)

I'm trying to write a Framework like Spring MVC.
I'm looking for a way to modify the code of the doPost/doGet method of a servlet 3.0 deployed on tomcat using javassist or reflection or whatever so the doPost can call a service method dynamically defined .
doPost(...){
ServiceClassName.methodeName(); // dynamic line of code
}
Thanks
It is possible using javassist. Refer this to write your Transformer class.
Although I have never tried it before, have a look at http://www.bytebuddy.net for a bytecode creation/manipulation library.

.net core equivalent for GetCustomAttribute on assembly?

I cannot find a .net core equivalent for calling GetCustomAttributes on an assembly object. I see there is CustomAttributes property, but this doesn't return instances of the custom attributes, but rather metadata about the attributes. How can I retrieve the actual attribute instance?
There's a GetCustomAttributes extension method. Just add using System.Reflection.

How to keep model attributes in a common place where more than one controllers can use it

I am new to spring framework. Can anyone resolve the below issue.
I have too many model attribute methodes(#ModelAttribute) in a controller say A, now I want to use all these model attributes in other controllers but I donot want to extend controller A, so there any way to keep all these model attributes in common place where all the controllers can execute this on each call.
In Spring 3.2 you can use the #ControllerAdvice annotation to create a class with model attribute (and init binder, and exception handler) methods that apply to all controllers.
In older versions, instead of extending controller A, cannot you move the annotated methods to a new class and make it the parent of all the controller classes?
You can utilize interceptors to achieve this. Interceptor has access to ModelAndView, so you should just put what you need to model, in one of appropriate methods provided by interceptor.
More details:
Using Spring Interceptors in your MVC Webapp
Spring MVC handler interceptors example
With spring 3.2 we have the option to specify the basepackage where we want to intercept with #ControllerAdvice. Below is the link
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/ControllerAdvice.html
I assume that you are having a common model attribute method and a common model attribute bean.
If the spring you are using is having an old version then the least you can do is separate out the body of the method in a common utility.
The other solution is inheritance , you can create a super controller that has one model attribute and then extend that controller for actual implementations that you want. Keep common functionality in super controller and specific implementations in child classes.

Virtual methods still required when using Moq?

We're using Moq and I was wondering what the current role of virtual methods were in it-- in the post below dated 2008 it's clear you had to mark your methods as virtual in order for Moq to work (or inherit from an interface.)
Moq discussion
However, is this still the case in .Net 4.5, that you're required to use virutal methods in the class you want to mock? And does this also hold true when you create a wrapper class around a static method-- the wrapper class either inherits from an interface or the method in question needs to be marked virtual?
This hasn't changed in .NET 4 or 4.5. As the link you provided explains, Moq uses Castle Windsor Dynamic Proxy to generate a type derived from the type you wish to mock. Therefore, the standard rules of inheritance apply. The derived type generated by Moq can only intercept calls to methods that any normal derived class can override.

Unity 2 Interception equivalent of CreateInterfaceProxyWithoutTarget in Castle Dynamic Proxy

I want to use Unity interception for an interface without an implementation, so that the IInterceptionBehavior actually becomes the implementation. This is the same as the CreateInterfaceProxyWithoutTarget method in Castle Dynamic Proxy.
Is this possible in Unity?
I think no, the only way I know to intercept an interface is with an InterfaceInterceptor but it requires a base class to be registered in the container:
Container.RegisterType<IRepository, BaseRepository>(
"repo1",
new Interceptor(new InterfaceInterceptor()),
new InterceptionBehavior(new RepoLoggingBehavior())
);
It's been a long while since this question was posted, but I came across it while trying to figure out just the same, and well, it seems like I've come up with something.
I've made it work by using Intercept.NewInstanceWithAdditionalInterfaces.
In my sample below I want a Unity to create an instance of a non defined class implementing IUserDao. All I want that instance of that Unity created class to do is defined in RetrieveSavedResultBehavior.
IUserDao userDao = (IUserDao)(Intercept.NewInstanceWithAdditionalInterfaces<Object>(
new VirtualMethodInterceptor(),
new List<IInterceptionBehavior>(){new RetrieveSavedResultBehavior()},
new List<Type>() { typeof(IUserDao) }
));
You can check my post regarding this topic here

Resources