Custom methods for use in spring security expression language annotations - spring-security

I have classes that extends Controller class
public class MyClass extends MainController {
...
}
And a MainController like this
#Controller
public abstract class MainController extends MultiActionController {
...
}
I would like to create a custom method like 'checkUser' to be used somehow like this:
#PreAuthorize("checkUser('attribute')")
public class MyClass extends MainController {
...
}
And add this method in MainController
#Controller
public abstract class MainController extends MultiActionController {
public boolean checkUser(String key) {
...
return true;
}
}
My question is this. If it is possible, how would I go about configuring it in the spring xml configuration files and come someone give me an example of a custom method used in this way?
Thanks

Related

Resolve singleton generics where T is a controller that implements interface

I have this situation:
public class MyController : Controller, ILoggable
{
private readonly ILogger<ILoggable> _logger;
public MyController(ILogger<ILoggable> logger)
{
this._logger = logger;
}
...
}
I want to resolve ILogger<ILoggable> as singleton and ILoggable as Transient.
In my startup I tried with this:
services.AddSingleton(typeof(ILogger<>), typeof(LoggerImpl<>));
But I am not able to resolve ILoggable because is a controller and I do not understand which is the best approach.
EDIT
Here is the problem:
I cannot declare ILogger<MyController> inside my controller because ILogger<ILoggable> is used in third part code. I try to explain better my problem without use dependency injiection.
I have a third part class that has a constructor like this>
Public ExternalClass(ILogger<ILoggable> logger) {…}
Now, in my controller I need to use this external class, but obviously I cannot do something like this:
public class MyController : Controller, ILoggable
{
private readonly ILogger<MyController> _logger;
public MyController(ILogger<MyController> logger)
{
this._logger = logger;
ExternalClass c = new ExternalClass(logger); //Here the error because logger is of type ILogger<MyController>, not ILogger<ILoggable>
}
...
}
I need to solve that via DI.
Can anyone help me please?
Thank you

Override View() in asp.net mvc

I have the follow base controller-
public abstract class BaseController : Controller
{
protected string BaseUrl = "URL";
}
All other controller inherit the above base controller-
public class MyController : BaseController
{
public ActionResult Something()
{
ViewBag.BaseUrl = base.BaseUrl;
return View();
}
}
I don't want to write ViewBag.BaseUrl = base.BaseUrl; in every controller action method. Rather, I would to automatically pass this base url to the related view. Is it possible by overriding View()?
An example would be better for me.
If all controllers derive this then just put it in here:
public abstract class BaseController : Controller
{
protected string BaseUrl = "URL";
public BaseController()
{
ViewBag.BaseUrl = base.BaseUrl;
}
}
I would even make it private if I do not want inheriting classes to overwrite it.

Child class of DbContext returns no data

Here's my sub-class of my DbContext:
public class JalsoxDbContextCache : JalsoxDbContext, IJalsoxDbContext
{
}
Any ideas why it would return no data where as if I use the base class instead it does?
The name of the class is used to create the connection string.
So I changed the derived class to this:
public class JalsoxDbContextCache : JalsoxDbContext, IJalsoxDbContext
{
public JalsoxDbContextCache()
: base("JalsoxDbContext")
{
}
}
How strange!

groovy :pass class(domain) as parameter and call static method dynmically

i have a tree of inheritance of Class
class Perfer{
String param
String value
}
class PerferChp extends Prefer{
static belongsTo=[chp:Chp]
}
class PerferSec extends Prefer{
static belongsTo=[sec:Sec]
}
When i try to pass domain class as parameter , i try this:
Prefer find(GormStaticApi<Prefer> preferenceFamily,String param){
preferenceFamily.findByParam(param);
}
But in vain

accessing HttpContext.Request in a controller's constructor

I'm following this ASP.NET MVC tutorial from Microsoft:
My code is slightly different, where I'm trying to access HttpContext.Request.IsAuthenticated in the controller's constructor.
namespace SCE.Controllers.Application
{
public abstract class ApplicationController : Controller
{
public ApplicationController()
{
bool usuario = HttpContext.Request.IsAuthenticated;
}
}
}
The problem is that HttpContext is always null.
Is there a solution to this?
instead of putting your HttpContext.Request.IsAuthenticated in Controller level you should put it in Controller Base class that will be inherited in all of your controller with an override method of OnActionExecuting() method.
In your Controller base you should have
public class BaseController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext ctx) {
base.OnActionExecuting(ctx);
ViewData["IsAuthenticated"] = HttpContext.Request.IsAuthenticated;
}
}
and all your Controller should inherit the BaseController class
public class ApplicationController : BaseController
now you should get the ViewData["IsAuthenticated"] in your Master page.
Edit
With the link you have given, and relating to what you have done, your ApplicationController is a Page Controller, not a Base Controller. In the example, ApplicationController is a Base Controller that is inherited by the HomeController but what you have done is you are placing the Action method inside your base controller which is the ApplicationController so your Action Index method will not be invoked when you call any page (Index page) that is not from the ApplicationController.
I would suggest you use:
System.Web.HttpContext.Current.Request
Just remember System.Web.HttpContext.Current is threadstatic, but if you don't use additional thread the solution works.
The Controller is instantiated significantly prior to the point where the Index action is invoked, and at the moment of construction HttpContext is indeed unavailable. What's wrong with referencing it in your controller method Index?
The solution of this problem is to create an override method of Initialize by passing RequestContext object.
public class ChartsController : Controller
{
bool isAuthed = false;
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
base.Initialize(requestContext);
if (requestContext.HttpContext.User.Identity.IsAuthenticated)
{
isAuthed =true;
}
}
}
With the answer I am posting here, you cannot access IsAuthenticated, but you can access some stuffs related to HttpContextRequest (see in image),
I needed session value in constructor.
You can use IHttpContextAccessor as below:
public ABCController(IHttpContextAccessor httpContextAccessor)
{
//do you stuff with httpContextAccessor,
// This gives session value
string abc = httpContextAccessor.HttpContext.Session.GetString("Abc");
}
and in startup.cs, you need to configure,
services.AddHttpContextAccessor();
It is possible to get the HttpContext using IHttpContextAccessor injected into class constructor. Before doing so, you will need first to register the corresponding service to the service container in Startup.cs class or Program.cs such as below.
services.AddHttpContextAccessor(); // Startup.cs
builder.Services.AddHttpContextAccessor(); // Program.cs
Right after that, you can inject the IHttpContextAccessor interface in whererever method or class constructor.
private bool isAuthenticated { get; set; }
public ConstructorName(IHttpContextAccessor accessor)
{
var context = accessor.HttpContext;
isAuthenticated = context.User.Identity.IsAuthenticated;
}

Resources