2014-04-12 6 views
10

में काम नहीं करता है जब मैं अपने पृष्ठ के भीतर इस तरह के कुछ टैग डालता हूं:
उपयोगकर्ता और व्यवस्थापक रनटाइम पर प्रदर्शित होंगे?
xmlns: सेकंड = "http://www.springframework.org/security/tags"
वसंत-सुरक्षा-taglibs-3.2.3.RELEASE
मैं 2 फ़ोल्डर (व्यवस्थापक और उपयोगकर्ता) है
इसके अलावा के साथ परीक्षण उपयोग-अभिव्यक्ति = "सत्य"
परिणाम नहीं मिला!
मैं mysql
तालिका (उपयोगकर्ताओं और user_roles) ...

सेकंड: प्राधिकरण वसंत सुरक्षा 3.2 और जेएसएफ

<sec:authorize access="ROLE_ADMIN"> 
     <div> test Admin</div> 
    </sec:authorize> 
    <sec:authorize access="ROLE_USER"> 
     <div> test User</div> 
    </sec:authorize> 
      or 
    <sec:authorize access="hasRole('ROLE_ADMIN')" > 
     <h:outputText value="Admin"/> 
    </sec:authorize> 
    <sec:authorize access="hasRole('ROLE_USER')" > 
     <h:outputText value="User"/> 
    </sec:authorize> 

Security.xml

<http auto-config="true" use-expressions="true" > 
     <intercept-url pattern="/Admin/*" access="hasRole('ROLE_ADMIN')" /> 
     <intercept-url pattern="/user/*" access="hasRole('ROLE_USER')"/> 
     ... 
</http> 

धन्यवाद इस्तेमाल किया!

उत्तर

2

स्प्रिंग सुरक्षा फेसलेट टैग लाइब्रेरी का उपयोग करने के लिए आपको एक .taglib.xml फ़ाइल बनाने और इसे web.xml में पंजीकृत करने की आवश्यकता होगी।

फ़ाइल बनाएँ /WEB-INF/springsecurity.taglib.xml निम्नलिखित सामग्री के साथ: आप

<context-param> 
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name> 
    <param-value>/WEB-INF/springsecurity.taglib.xml</param-value> 
</context-param> 

अब:

<?xml version="1.0"?> 
<!DOCTYPE facelet-taglib PUBLIC 
"-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" 
"http://java.sun.com/dtd/facelet-taglib_1_0.dtd"> 
<facelet-taglib> 
    <namespace>http://www.springframework.org/security/tags</namespace> 
    <tag> 
     <tag-name>authorize</tag-name> 
     <handler-class>org.springframework.faces.security.FaceletsAuthorizeTagHandler</handler-class> 
    </tag> 
    <function> 
     <function-name>areAllGranted</function-name> 
     <function-class>org.springframework.faces.security.FaceletsAuthorizeTagUtils</function-class> 
     <function-signature>boolean areAllGranted(java.lang.String)</function-signature> 
    </function> 
    <function> 
     <function-name>areAnyGranted</function-name> 
     <function-class>org.springframework.faces.security.FaceletsAuthorizeTagUtils</function-class> 
     <function-signature>boolean areAnyGranted(java.lang.String)</function-signature> 
    </function> 
    <function> 
     <function-name>areNotGranted</function-name> 
     <function-class>org.springframework.faces.security.FaceletsAuthorizeTagUtils</function-class> 
     <function-signature>boolean areNotGranted(java.lang.String)</function-signature> 
    </function> 
    <function> 
     <function-name>isAllowed</function-name> 
     <function-class>org.springframework.faces.security.FaceletsAuthorizeTagUtils</function-class> 
     <function-signature>boolean isAllowed(java.lang.String, java.lang.String)</function-signature> 
    </function> 
</facelet-taglib> 

इसके बाद, रजिस्टर web.xml में ऊपर फ़ाइल taglib आपके विचारों में टैग लाइब्रेरी का उपयोग करने के लिए तैयार हैं।

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:sec="http://www.springframework.org/security/tags"> 

    <sec:authorize ifAllGranted="ROLE_FOO, ROLE_BAR"> 
     Lorem ipsum dolor sit amet 
    </sec:authorize> 

    <sec:authorize ifNotGranted="ROLE_FOO, ROLE_BAR"> 
     Lorem ipsum dolor sit amet 
    </sec:authorize> 

    <sec:authorize ifAnyGranted="ROLE_FOO, ROLE_BAR"> 
     Lorem ipsum dolor sit amet 
    </sec:authorize> 

</ui:composition> 

आप किसी भी JSF घटक के प्रदान की गई या अन्य विशेषता में कई ईएल कार्यों में से एक का उपयोग कर सकते हैं::

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:sec="http://www.springframework.org/security/tags"> 

    <!-- Rendered only if user has all of the listed roles --> 
    <h:outputText value="Lorem ipsum dolor sit amet" rendered="#{sec:areAllGranted('ROLE_FOO, ROLE_BAR')}"/> 

    <!-- Rendered only if user does not have any of the listed roles --> 
    <h:outputText value="Lorem ipsum dolor sit amet" rendered="#{sec:areNotGranted('ROLE_FOO, ROLE_BAR')}"/> 

    <!-- Rendered only if user has any of the listed roles --> 
    <h:outputText value="Lorem ipsum dolor sit amet" rendered="#{sec:areAnyGranted('ROLE_FOO, ROLE_BAR')}"/> 

    <!-- Rendered only if user has access to given HTTP method/URL as defined in Spring Security configuration --> 
    <h:outputText value="Lorem ipsum dolor sit amet" rendered="#{sec:isAllowed('/secured/foo', 'POST')}"/> 

</ui:composition> 

पर परीक्षण किया गया:

आप नेस्टेड सामग्री सशर्त शामिल करने के लिए अधिकृत टैग का उपयोग कर सकते
 <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-taglibs</artifactId> 
      <version>3.2.3.RELEASE</version> 
     </dependency> 
+0

hi आपके उत्तर के लिए धन्यवाद ... – user2038046

+0

@ user2038046 उत्तर स्पष्ट नहीं है या आप एक और पाया है? – 0x5a4d

संबंधित मुद्दे