2012-08-07 19 views
21

मुझे कई समान प्रश्न मिले लेकिन किसी ने भी मेरी समस्या हल नहीं की है। मेरी समस्या ROLE_USERROLE_ADMIN@ प्रेरणा प्राधिकृत वसंत सुरक्षा

मेरे वसंत-सुरक्षा.एक्सएमएल कोड का पालन कर रहा है।

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:s="http://www.springframework.org/schema/security" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    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.0.xsd"> 

<s:http auto-config="true" use-expressions="true"> 
    <s:intercept-url pattern="/index.jsp" access="permitAll" /> 
    <s:intercept-url pattern="/welcome*" access="hasRole('ROLE_USER')" /> 
    <s:intercept-url pattern="/helloadmin*" access="hasRole('ROLE_ADMIN')" /> 

    <s:form-login login-page="/login" default-target-url="/welcome" 
     authentication-failure-url="/loginfailed" /> 
    <s:logout logout-success-url="/logout" /> 
</s:http> 

<s:authentication-manager> 
    <s:authentication-provider> 
    <s:user-service> 
     <s:user name="asif" password="123456" authorities="ROLE_USER,ROLE_ADMIN" /> 
     <s:user name="raheel" password="123456" authorities="ROLE_USER" />   
    </s:user-service> 
    </s:authentication-provider> 
</s:authentication-manager> 

जब मैं <s:global-method-security pre-post-annotations="enabled"/> मेरी कोड जोड़ने के संसाधन नहीं मिला त्रुटि दिखाता है और जब मैं अपने कोड को दूर सफलतापूर्वक निष्पादित लेकिन ROLE_USERROLE_ADMIN कार्यों

मेरे नियंत्रक समारोह है पहुँच सकते हैं।

@PreAuthorize("hasRole('ROLE_ADMIN')") 
@RequestMapping(value="/delete", method = RequestMethod.GET) 
public String DeleteAll(ModelMap model, Principal principal) { 

    org.springframework.security.core.userdetails.User activeUser = (org.springframework.security.core.userdetails.User)SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 
    System.out.println("Active user is "+activeUser.getUsername()+"Authorities are "+activeUser.getAuthorities()); 
    return "deleteUsers"; 

} 

उत्तर

21

आप

<s:global-method-security pre-post-annotations="enabled"/> 

आप @PreAuthorize एनोटेशन काम करने के लिए चाहते हैं होना चाहिए।

ऐसा लगता है कि आप spring-aop निर्भरता भूल रहे हैं की तरह:


टिप्पणी का उत्तर दें।

आप Maven उपयोग कर रहे हैं आप की जरूरत:

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-aop</artifactId> 
    <version>${org.springframework.version}</version> 
</dependency> 

नहीं आप here से जार प्राप्त कर सकते हैं।

+0

हाँ मैं जानता हूँ, लेकिन जब मैं जोड़ने मेरी कोड संसाधन, त्रुटि नहीं मिला पता चलता है क्या तुम जानते हो कि यह कैसे तय करने के लिए? – Raheel

+0

क्लास org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanDefinitionStoreException: श्रोता कॉन्टेक्स्ट संसाधन से XML दस्तावेज़ को अनपेक्षित अपवाद पार्सिंग [/WEB-INF/spring-security.xml ]; नेस्टेड अपवाद java.lang.No.NoassassDefFoundError: org/aopalliance/intercept/methodInterceptor – Raheel

+0

और मैं वसंत सुरक्षा 3.07 – Raheel

3

@Secured टिप्पणी के साथ प्रयास करें,

तो फिर तुम

@Secured("ROLE_ADMIN") 
@RequestMapping(value="/delete", method = RequestMethod.GET) 
public String DeleteAll(ModelMap model, Principal principal) { 

    ... 

} 

यहाँ a detailed blog post about it है होगा।

1

यह आपकी मदद कर सकता है:

<security:global-method-security secured-annotations="enabled" proxy-target-class="true"/> 

सादर।

+0

बड़ा धन्यवाद का उपयोग कर रहा हूं! यह मेरी समस्याओं का हल किया। – WhiteWater

2

उपर्युक्त उत्तरों में से कोई भी मेरे लिए काम नहीं किया है। मुझे सुरक्षा सजावटी जोड़ने का मार्ग जाना पड़ा।

सजावटी servlet-context.xml फ़ाइल में सेम पर रखे गए हैं।

पहले XML नामस्थान के लिए सुरक्षा स्कीमा जोड़ें:

<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
... 
xmlns:security="http://www.springframework.org/schema/security" 
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 

फिर तो जैसे, सेवा की फलियों के कार्यान्वयन के लिए डेकोरेटर लागू ..

<beans:bean id="myService" class="my.service.impl.MyServiceImpl" scope="request"> 
    <security:intercept-methods> 
     <security:protect access="ROLE_USER" method="get*"/> 
     <security:protect access="ROLE_ADMIN" method="set*"/> 
    </security:intercept-methods> 
</beans:bean> 
1

यह समस्या सर्वलेट का उपयोग करते समय पैदा होगा वेब Async समर्थन के साथ 3। एक बार Callable विधि की अज्ञात विधि का मनोरंजन करने के बाद वसंत सुरक्षा 3.1.4 और नीचे उनके सुरक्षा संदर्भ को खो देते हैं।

Spring Security to 3.2.0 RC1 का उन्नयन आपकी समस्याओं का समाधान करेगा।

Maven निर्भरता:

<dependencies> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-web</artifactId> 
     <version>3.2.0.RC1</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-config</artifactId> 
     <version>3.2.0.RC1</version> 
    </dependency> 
</dependencies> 

<repositories> 
    <repository> 
     <id>spring-milestones</id> 
     <name>Spring Milestones</name> 
     <url>http://repo.spring.io/milestone</url> 
     <snapshots> 
      <enabled>false</enabled> 
     </snapshots> 
    </repository> 
</repositories> 
<repositories> 
    <repository> 
     <id>spring-milestones</id> 
     <name>Spring Milestones</name> 
     <url>http://repo.spring.io/milestone</url> 
     <snapshots> 
      <enabled>false</enabled> 
     </snapshots> 
    </repository> 
</repositories> 
13

मैं एक ही मुद्दे का सामना करना पड़ रहा था। मेरी समस्या हल हो गई जब मैंने नीचे दिए गए तत्व को applicationContext.xml से * -servlet.xml (मेरे प्रेषक की कॉन्फ़िगरेशन xml) में स्थानांतरित कर दिया।

<security:global-method-security secured-annotations="enabled"/> 

आपको अपने आवेदन की एक्सएमएल पर नहीं अपने प्रेषक की एक्सएमएल पर इस तत्व शामिल करने के लिए की है।
Spring FAQ

+1

यदि आपके पास कम से कम ग्लोबल-सुरक्षा-विधि आपके सर्वलेट के xml config- – chrismarx

+0

में परिभाषित नहीं है, तो यह निश्चित रूप से सही जवाब है, यह सटीक समस्या थी, स्पष्टीकरण के लिए स्प्रिंग FAQ लिंक का उल्लेख करने के लिए +1। –

21

आप XML विन्यास का उपयोग कर रहे हैं, तो मत भूलना निम्नलिखित विशेषता जोड़ने के लिए:

<s:global-method-security pre-post-annotations="enabled"/>

आप जावा विन्यास का उपयोग कर रहे हैं, तो निम्न टिप्पणी जोड़ने के लिए भूल नहीं है:

@EnableGlobalMethodSecurity(prePostEnabled = true)

0

वसंत सुरक्षा कॉन्फ़िगरेशन फ़ाइल में नहीं, अन्य वसंत कॉन्फ़िगरेशन फ़ाइल में टैग नीचे डाला गया ई।

मुझे भी वही त्रुटि मिल रही थी।

<security:global-method-security secured-annotations="enabled" proxy-target-class="true"/> 
संबंधित मुद्दे