How to get hold of user details in custom expression handlers

孤者浪人 提交于 2019-12-11 17:57:15

问题


I am using a custom security expression handler and using spring 3.2.0. Here is the custom expression root class :

public class CustomerPortalSecurityExpressionRoot extends WebSecurityExpressionRoot {

    private static final Log logger = LogFactory.getLog(CustomerPortalSecurityExpressionRoot.class);

    private CustomerPortalPanicService customerPortalPanicService;

    public CustomerPortalSecurityExpressionRoot(Authentication a, FilterInvocation fi) {
        super(a, fi);
    }

    public boolean isPanicking() {
        if (customerPortalPanicService != null) {

            return customerPortalPanicService.isPanicking();
        } else {
            logger.warn("CustomerPortalPanicService is not available.");
            return false;
        }
    }


    public boolean hasGotPermission(String title){


       logger.debug("coming inside has Permission! @public class CustomerPortalSecurityExpressionRoot "+title);
        return true;
    }
    public void setCustomerPortalPanicService(CustomerPortalPanicService customerPortalPanicService) {
        this.customerPortalPanicService = customerPortalPanicService;
    }
}

I am using it this way in a spring security config file :

 <http auto-config="true" use-expressions="true" >

        <form-login login-page="/login" login-processing-url="/loginIFM" authentication-failure-url="/login/?login_error=1" username-parameter="username" password-parameter="password" />
        <logout invalidate-session="true" logout-success-url="/" logout-url="/logout_ifm" />

         <expression-handler ref="webSecurityExpressionHandler"/>

        <!-- Rules. -->
        <!--     <intercept-url pattern="/" access="permitAll" /> -->

        <intercept-url pattern="/hardcopy/*" access="isAuthenticated() and hasPermission('tw')" />
    </http>

 <!-- expression custom handler -->
    <b:bean id="webSecurityExpressionHandler" class="no.user.security.DnWebSecurityExpressionHandler" />

The authentication is taking place using authentication manager, I just want to know that how could I get hold of that user details which is coming as a JSON response after authentication? I know that there is a hasPermission thing in PermissionEvaluator, but this is much more flexible for me. Help!


回答1:


You can use SecurityContextHolder.getContext().getAuthentication().getAuthorities() to get a hold of the authorities granted to the currently authenticated user.



来源:https://stackoverflow.com/questions/16566118/how-to-get-hold-of-user-details-in-custom-expression-handlers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!