का उपयोग कर अनधिकृत आरईएसटी सेवा अनुरोध पर जेएसओएन लौटें, मैं वसंत सुरक्षा का उपयोग कर विधि स्तर प्रतिबंध/प्राधिकरण (आरईएसटी सेवाओं पर) लागू कर रहा हूं।
मैंने वसंत अभिव्यक्ति भाषा का उपयोग किया है और कस्टम अभिव्यक्ति मूल्यांकनकर्ता लागू किया है।स्प्रिंग सिक्योरिटी 'हैप्रमिशन()'
यह मेरे लिए ठीक काम कर रहा है। हालांकि, अगर एक अनधिकृत उपयोगकर्ता सेवा तक पहुंचने का प्रयास करता है, तो यह एक लॉगिन पेज का जवाब दे रहा है। चूंकि मेरा आवेदन REST आधारित केवल है, इसलिए मैं सभी अनुरोधों के लिए केवल JSON डेटा वापस करना चाहता हूं।
मैं कैसे करूँ यह JSON लौट प्रवेश पृष्ठ के बजाय बनाने के (जैसे: {status : Denied}
)?
यहाँ कोड स्निपेट है:
CustomEvaluator
public boolean hasPermission(Authentication authentication, Object userId, Object permissionId) {
List<String> permList = new MyDAO().getPermissionsForUser((String) userId);
if(permList.contains(((String) permissionId))){
return true;
}else{
return false;
}
}
सेवा
@PreAuthorize("hasPermission(#userId, '3')")
public String testAuthorization(Object obj, String userId){
System.out.println("Has access to the service method....");
return "success";
}
नियंत्रक
public @ResponseBody String testAuthorization(Object o,@RequestParam("userId") String userId){
System.out.println("User ID = "+userId);
String abc = service.testAuthorization(o,userId);
return "{\"status\":\"success\"}";
}
वसंत-security.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:security="http://www.springframework.org/schema/security"
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">
<security:global-method-security secured-annotations="enabled" pre-post-annotations="enabled">
<security:expression-handler ref="expressionHandler"/>
</security:global-method-security>
<!-- This is where we configure Spring-Security -->
<security:http auto-config="true" use-expressions="true" access-denied-page="/auth/auth/denied" >
<security:intercept-url pattern="/auth/auth/login" access="permitAll"/>
<security:intercept-url pattern="/auth/main/admin" access="hasRole('ROLE_ADMIN')"/>
<security:intercept-url pattern="/auth/main/common" access="hasRole('ROLE_USER')"/>
</security:http>
<security:authentication-manager>
<security:authentication-provider user-service-ref="customUserDetailsService">
<security:password-encoder ref="passwordEncoder"/>
</security:authentication-provider>
</security:authentication-manager>
<!-- Use a Md5 encoder since the user's passwords are stored as Md5 in the database -->
<bean class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" id="passwordEncoder"/>
<!-- A custom service where Spring will retrieve users and their corresponding access levels -->
<bean id="customUserDetailsService" class="com.cjl.security.service.CustomUserDetailsService"/>
<bean id="expressionHandler" class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler">
<property name="permissionEvaluator" ref="permissionEvaluator"/>
</bean>
<bean id="permissionEvaluator" class="com.cjl.security.evaluators.MethodPermissionEvaluator"/>
</beans>
मैं वसंत-सुरक्षा संस्करण 3.0.5 का उपयोग कर रहा हूं। मैंने अपने नियंत्रक को @ExceptionHandler (AccessDeniedException.class) जोड़ा। अभी भी एक ही लॉगिन पेज प्राप्त हो रहा है। –
क्षमा करें, मेरी गलती। यह अब काम कर रहा है। 3.0.5 संस्करण में, अगर मुझे एक और नियंत्रक में एक ही अपवाद को संभालना है, तो रास्ता क्या है? अपवाद हैंडलिंग डुप्लिकेट करना एकमात्र तरीका है? –
आप या तो हैंडलर डुप्लिकेट कर सकते हैं, या आप एक अमूर्त नियंत्रक बना सकते हैं जो आपके सभी अन्य नियंत्रकों का विस्तार होता है। आप भी एक पहलू आईटीडी बनाने में सक्षम हो सकते हैं जो आपके सभी नियंत्रकों के लिए हैंडलर घोषित करता है, हालांकि मैं मानता हूं कि मैंने कोशिश नहीं की है। – DavidA