Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating New Roles and Permissions Dynamically in Spring Security 3
    primarykey
    data
    text
    <p>I am using Spring Security 3 in Struts 2 + Spring IOC project. </p> <p>I have used Custom Filter, Authentication Provider etc. in my Project. </p> <p>You can see my security.xml here</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"&gt; &lt;global-method-security pre-post-annotations="enabled"&gt; &lt;expression-handler ref="expressionHandler" /&gt; &lt;/global-method-security&gt; &lt;beans:bean id="expressionHandler" class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler" &gt; &lt;beans:property name="permissionEvaluator" ref="customPermissionEvaluator" /&gt; &lt;/beans:bean&gt; &lt;beans:bean class="code.permission.MyCustomPermissionEvaluator" id="customPermissionEvaluator" /&gt; &lt;!-- User Login --&gt; &lt;http auto-config="true" use-expressions="true" pattern="/user/*" &gt; &lt;intercept-url pattern="/index.jsp" access="permitAll"/&gt; &lt;intercept-url pattern="/user/showLoginPage.action" access="permitAll"/&gt; &lt;intercept-url pattern="/user/showFirstPage" access="hasRole('ROLE_USER') or hasRole('ROLE_VISIT')"/&gt; &lt;intercept-url pattern="/user/showSecondUserPage" access="hasRole('ROLE_USER')"/&gt; &lt;intercept-url pattern="/user/showThirdUserPage" access="hasRole('ROLE_VISIT')"/&gt; &lt;intercept-url pattern="/user/showFirstPage" access="hasRole('ROLE_USER') or hasRole('ROLE_VISIT')"/&gt; &lt;form-login login-page="/user/showLoginPage.action" /&gt; &lt;logout invalidate-session="true" logout-success-url="/" logout-url="/user/j_spring_security_logout"/&gt; &lt;access-denied-handler ref="myAccessDeniedHandler" /&gt; &lt;custom-filter before="FORM_LOGIN_FILTER" ref="myApplicationFilter"/&gt; &lt;/http&gt; &lt;beans:bean id="myAccessDeniedHandler" class="code.security.MyAccessDeniedHandler" /&gt; &lt;beans:bean id="myApplicationFilter" class="code.security.MyApplicationFilter"&gt; &lt;beans:property name="authenticationManager" ref="authenticationManager"/&gt; &lt;beans:property name="authenticationFailureHandler" ref="failureHandler"/&gt; &lt;beans:property name="authenticationSuccessHandler" ref="successHandler"/&gt; &lt;/beans:bean&gt; &lt;beans:bean id="successHandler" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler"&gt; &lt;beans:property name="defaultTargetUrl" value="/user/showFirstPage"&gt; &lt;/beans:property&gt; &lt;/beans:bean&gt; &lt;beans:bean id="failureHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"&gt; &lt;beans:property name="defaultFailureUrl" value="/user/showLoginPage.action?login_error=1"/&gt; &lt;/beans:bean&gt; &lt;beans:bean id= "myUserDetailServiceImpl" class="code.security.MyUserDetailServiceImpl"&gt; &lt;/beans:bean&gt; &lt;beans:bean id="myAuthenticationProvider" class="code.security.MyAuthenticationProvider"&gt; &lt;beans:property name="userDetailsService" ref="myUserDetailServiceImpl"/&gt; &lt;/beans:bean&gt; &lt;!-- User Login Ends --&gt; &lt;!-- Admin Login --&gt; &lt;http auto-config="true" use-expressions="true" pattern="/admin/*" &gt; &lt;intercept-url pattern="/index.jsp" access="permitAll"/&gt; &lt;intercept-url pattern="/admin/showSecondLogin" access="permitAll"/&gt; &lt;intercept-url pattern="/admin/*" access="hasRole('ROLE_ADMIN')"/&gt; &lt;form-login login-page="/admin/showSecondLogin"/&gt; &lt;logout invalidate-session="true" logout-success-url="/" logout-url="/admin/j_spring_security_logout"/&gt; &lt;access-denied-handler ref="myAccessDeniedHandlerForAdmin" /&gt; &lt;custom-filter before="FORM_LOGIN_FILTER" ref="myApplicationFilterForAdmin"/&gt; &lt;/http&gt; &lt;beans:bean id="myAccessDeniedHandlerForAdmin" class="code.security.admin.MyAccessDeniedHandlerForAdmin" /&gt; &lt;beans:bean id="myApplicationFilterForAdmin" class="code.security.admin.MyApplicationFilterForAdmin"&gt; &lt;beans:property name="authenticationManager" ref="authenticationManager"/&gt; &lt;beans:property name="authenticationFailureHandler" ref="failureHandlerForAdmin"/&gt; &lt;beans:property name="authenticationSuccessHandler" ref="successHandlerForAdmin"/&gt; &lt;/beans:bean&gt; &lt;beans:bean id="successHandlerForAdmin" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler"&gt; &lt;/beans:bean&gt; &lt;beans:bean id="failureHandlerForAdmin" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"&gt; &lt;beans:property name="defaultFailureUrl" value="/admin/showSecondLogin?login_error=1"/&gt; &lt;/beans:bean&gt; &lt;authentication-manager alias="authenticationManager"&gt; &lt;authentication-provider ref="myAuthenticationProviderForAdmin" /&gt; &lt;authentication-provider ref="myAuthenticationProvider" /&gt; &lt;/authentication-manager&gt; &lt;beans:bean id="myAuthenticationProviderForAdmin" class="code.security.admin.MyAuthenticationProviderForAdmin"&gt; &lt;beans:property name="userDetailsService" ref="userDetailsServiceForAdmin"/&gt; &lt;/beans:bean&gt; &lt;beans:bean id= "userDetailsServiceForAdmin" class="code.security.admin.MyUserDetailsServiceForAdminImpl"&gt; &lt;/beans:bean&gt; &lt;!-- Admin Login Ends --&gt; &lt;beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"&gt; &lt;beans:property name="basenames"&gt; &lt;beans:list&gt; &lt;beans:value&gt;code/security/SecurityMessages&lt;/beans:value&gt; &lt;/beans:list&gt; &lt;/beans:property&gt; &lt;/beans:bean&gt; </code></pre> <p></p> <p>Uptill now you can see, url-pattern I have mentioned is hard coded. I wanted to know if there is a way to create new ROLES and PERMISSIONS dynamically, not hard coded.</p> <p>Like creating new roles and permissions and saving them to database and then accessing from database. I have searched on net, but I am not able to find out how to add new entries to code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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