मैं एक स्प्रिंग प्रोजेक्ट में स्प्रिंग सुरक्षा जोड़ रहा हूं। सिस्टम का आर्किटेक्चर आरईएसटी है और उपयोगकर्ता विभिन्न संसाधनों तक पहुंच सकता है।@PreAuthorize विधि सुरक्षा नियमों और विधि पैरामीटर के साथ काम नहीं करता
मैं इस जानकारी के मालिकों और उपयोगकर्ताओं को व्यक्तिगत जानकारी तक पहुंच देना चाहता हूं। मैं साधारण शुरू कर दिया है: इस तरह उपयोगकर्ता प्रोफ़ाइल को छानने:
मेरी सेवा परत मैं विधि व्याख्या का उपयोग करना चाहता था और विधि पैरामीटर शामिल में ..
@PreAuthorize("hasRole('ROLE_ADMIN') or principal.userId == #id")
public Usuario getUser(int id) throws DAOException {
...
}
लेकिन इस सब पर काम नहीं कर रहा। (वेब परत) किसी भी उपयोगकर्ता सभी प्रोफ़ाइल देख सकते हैं (व्यवस्थापक और सभी उपयोगकर्ताओं को भी) इस URL अनुरोध किया जाता है:
@RequestMapping(value="/user/{uid}", method=RequestMethod.GET)
public ModelAndView getUser(@PathVariable int uid) throws DAOException {
userDAO = new UsuarioJPADAO();
userService.setUsuarioDAO(userDAO);
return new ModelAndView("user", "user", userService.getUser(uid));
}
यहाँ मेरी security.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<!-- Security Annotations -->
<global-method-security
pre-post-annotations="enabled"/>
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/css/**" access="permitAll" />
<intercept-url pattern="/images/**" access="permitAll" />
<intercept-url pattern="/js/**" access="permitAll" />
<intercept-url pattern="/favicon.ico" access="permitAll" />
<intercept-url pattern="/login" access="permitAll" />
<intercept-url pattern="/users" access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="https://stackoverflow.com/users/page/*" access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="/customers" access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="/employees" access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="/search/*" access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="/*" access="hasAnyRole('ROLE_ADMIN, ROLE_EMPLOYEE, ROLE_PARTNER, ROLE_USER')" />
<intercept-url pattern="/*/*" access="hasAnyRole('ROLE_USER, ROLE_ADMIN')" />
<intercept-url pattern="/*/*/*" access="hasAnyRole('ROLE_USER, ROLE_ADMIN')" />
<intercept-url pattern="/*/*/*/*" access="hasAnyRole('ROLE_USER, ROLE_ADMIN')" />
<intercept-url pattern="/*/*/*/*/*" access="hasAnyRole('ROLE_USER, ROLE_ADMIN')" />
<intercept-url pattern="/*/*/*/*/*/*" access="hasAnyRole('ROLE_USER, ROLE_ADMIN')" />
<intercept-url pattern="/*/*/*/*/*/*/*" access="hasAnyRole('ROLE_USER, ROLE_ADMIN')" />
<form-login login-page="/login" login-processing-url="/doLogin"
authentication-failure-url="/login?error"
username-parameter="username" password-parameter="password"
default-target-url="/default" />
<logout invalidate-session="true" logout-success-url="/login?logout" logout-url="/logout"/>
</http>
<authentication-manager>
<authentication-provider user-service-ref="UsuarioService">
</authentication-provider>
</authentication-manager>
मैं है Spring Security 3.1 book की जांच की है और स्पष्ट रूप से मेरी कॉन्फ़िगरेशन पुस्तक के सुझाव के रूप में है। मैंने अन्य स्टैक ओवरफ़्लो पोस्ट (here और here) पढ़े हैं लेकिन मुझे कोई भाग्य नहीं था।
अद्यतन: जोड़ा गया application-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<context:annotation-config />
<context:component-scan base-package="com.pe.fs" />
<mvc:annotation-driven />
<mvc:resources mapping="/**" location="/" />
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>
<!-- DataSource -->
<bean id="jpaDataSource" class="oracle.jdbc.pool.OracleDataSource"
destroy-method="close"
p:driverType="oracle.jdbc.OracleDriver"
p:user="**********"
p:password="**********"
p:uRL="jdbc:oracle:thin:@localhost:1521:XE"
/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"></property>
<property name="persistenceUnitName" value="freesunPU" />
<property name="dataSource" ref="jpaDataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
<property name="showSql" value="false" />
</bean>
</property>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory" />
<tx:annotation-driven mode="aspectj"/>
<context:load-time-weaver aspectj-weaving="autodetect" />
अद्यतन: मैं पोम और कोई बदलाव नहीं करने के लिए spring-security-aspects
जोड़ लिया है। उत्तरों में सुझाए गए अन्य परिवर्तनों का परीक्षण किया गया है लेकिन एनोटेशन जैसे @PreAuthorize
अभी भी काम नहीं कर रहे हैं। सीएनए यह संदर्भों के बीच एक समस्या है? पहलूजे का कारण क्या हो सकता है?
मैं क्या गलत कर रहा हूं?
public interface UserService extends UserDetailsService {
Usuario getUser(int id) throws DAOException
}
अपने उपयोगकर्ता सेवा में लागू करें और दोबारा कोशिश:
आपका 'usuario getUser (पूर्णांक आईडी)' विधि कुछ इंटरफ़ेस में परिभाषित किया गया है? डिफ़ॉल्ट रूप से एनोटेशन इस तरह का काम कर सकते हैं नहीं (JDK proxieas क्रम में प्राधिकरण चेकों को जोड़ने के लिए उपयोग किया जाता है और वे केवल इंटरफ़ेस तरीकों लक्षित कर सकते हैं) –
@MaksymDemidas नहीं। मेरे UsuarioService UserDetailsService लागू करता है। मैं अपने स्वयं के UsuarioDetails क्लास के साथ भी काम कर रहा हूं जो मेरे डोमेन Usuario को बढ़ाता है और उपयोगकर्ता विवरण – Spacemonkey
wft man लागू करता है? :) <अवरोधन-यूआरएल ढांचा = "/ */*/*/*/*" पहुँच = "hasAnyRole ('ROLE_USER, ROLE_ADMIN')" /> परिवर्तन ""/*/*/*/*/* "" करने के लिए "/ **" – Yura