मुझे स्प्रिंग के साथ मूल स्वामित्व का उपयोग करते समय HTTP प्रतिक्रिया शीर्षलेख "एक्सेस-कंट्रोल-ऑब्जेक्ट-ओरिजिन" से संबंधित कोई समस्या मिली। जब मैं मैन्युअल रूप से प्रमाणित करते हैं, कोड bellow (मैं बाकी उपयोग कर रहा हूँ) की तरह:स्प्रिंग सिक्योरिटी, आरईएसटी मूल प्रमाणीकरण समस्या
@RequestMapping(value = "/login", method = RequestMethod.POST, consumes = "application/json")
@ResponseStatus(value = HttpStatus.OK)
public void login(@RequestBody String body, HttpServletResponse response)
throws IOException {
try {
User user = gson.fromJson(body, User.class);
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
usuario.getUsername(), usuario.getPassword());
authenticationManager.authenticate(token);
} catch (BadCredentialsException e) {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
} catch (Exception e) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
सब कुछ ठीक काम करता है, मैं निम्नलिखित HTTP प्रतिक्रिया प्राप्त करते हैं:
HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: null
Access-Control-Allow-Credentials: true
Content-Type: text/html;charset=utf-8
Content-Length: 951
Date: Fri, 17 May 2013 19:14:36 GMT
जैसा कि आप देख सकते हैं, "Access- नियंत्रण-अनुमति-उत्पत्ति "प्रतिक्रिया पर मौजूद है। सबकुछ ठीक है। मैं अपने AJAX कॉल में 401 त्रुटि पकड़ सकता हूं।
लेकिन जब प्रमाणीकरण bellow कोड की तरह है, स्वचालित रूप से किया जाता है:
@RequestMapping(value = "/name", method = RequestMethod.POST, consumes = "application/json")
@PreAuthorize("hasRole('ROLE_CUSTOMER')")
public @ResponseBody String getName(HttpServletResponse response) throws IOException {
String json = null;
try {
User userSession = (User) SecurityContextHolder.getContext()
.getAuthentication().getPrincipal();
Customer customer = customerDao.getNameByUsername(userSession.getUsername());
json = gson.toJson(customer);
} catch (Exception e) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
return json;
}
HTTP प्रतिक्रिया है:
HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
WWW-Authenticate: Basic realm="Spring Security Application"
Content-Type: text/html;charset=utf-8
Content-Length: 981
Date: Fri, 17 May 2013 19:41:08 GMT
नहीं "पहुंच-नियंत्रण-अनुमति दें-उत्पत्ति" वहाँ है में
Origin null is not allowed by Access-Control-Allow-Origin
: प्रतिक्रिया
गूगल क्रोम कंसोल निम्न त्रुटि दिखाने
मेरा AJAX कॉल 401 अनधिकृत त्रुटि नहीं लौटाता है, भले ही HTTP प्रतिक्रिया इसे वापस करे (ऊपर प्रतिक्रिया), मुझे एक अनजान त्रुटि प्राप्त होती है।
मुझे पता चला कि सभी ब्राउज़रों के लिए, मुझे HTTP प्रतिक्रिया में "एक्सेस-कंट्रोल-ऑब्जेक्ट-ऑरिजन" की आवश्यकता है, अन्यथा वे किसी प्रकार की मूक त्रुटि उत्पन्न करेंगे और मेरा AJAX कॉल विफल हो जाएगा (पकड़ नहीं सकता 401 त्रुटि)। असल में, जावास्क्रिप्ट चुपचाप विफल हो जाएगा। XMLHttpRequest "एक्सेस-कंट्रोल-अनुमति-उत्पत्ति" के बिना HTTP प्रतिक्रिया स्वीकार नहीं करता है।
मैं बुनियादी प्रमाणीकरण के लिए HTTP प्रतिक्रियाओं में "एक्सेस-कंट्रोल-ऑब्जेक्ट-उत्पत्ति" कैसे वसंत कर सकता हूं?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="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.2.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<security:http create-session="stateless" entry-point-ref="authenticationEntryPoint">
<security:intercept-url pattern="/customer/**" />
<security:http-basic />
<security:custom-filter ref="basicAuthenticationFilter"
after="BASIC_AUTH_FILTER" />
</security:http>
<bean id="basicAuthenticationFilter"
class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter">
<property name="authenticationManager" ref="authenticationManager" />
<property name="authenticationEntryPoint" ref="authenticationEntryPoint" />
</bean>
<bean id="authenticationEntryPoint"
class="org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint">
<property name="realmName" value="teste.com" />
</bean>
<!-- It is responsible for validating the user's credentials -->
<security:authentication-manager alias="authenticationManager">
<!-- It is responsible for providing credential validation to the AuthenticationManager -->
<security:authentication-provider>
<security:password-encoder ref="passwordEncoder" />
<security:jdbc-user-service
data-source-ref="mySQLdataSource"
users-by-username-query="select username, password, enabled from usuario where username = ?"
authorities-by-username-query="select username, papel from autoridade where username = ?" />
</security:authentication-provider>
</security:authentication-manager>
<bean class="org.springframework.security.crypto.password.StandardPasswordEncoder"
id="passwordEncoder" />
</beans>
धन्यवाद आदमी। आपके समाधान में मदद की। –
हाय, मैं आपको निम्नलिखित से पूछना चाहता हूं। मैं जानना चाहता हूं कि क्रॉस अनुरोध + मूल प्राधिकरण + वसंत सुरक्षा सक्षम करने के लिए कुछ विशिष्ट है या नहीं। अब तक मैंने टॉमकैट फ़िल्टर (मैंने हेडर को प्राधिकरण जोड़ा है) सेट करने के लिए आवश्यक सबकुछ किया है, मैं AJAX में अपना अनुरोध करता हूं जैसा कि यह होना चाहिए, यह देखते हुए कि यह प्रमाणीकरण के बिना काम करता है, इसलिए मुझे आश्चर्य है कि वसंत सुरक्षा कोई समस्या नहीं पैदा कर रही है या नहीं ..... हो सकता है कि आपके पास इसके बारे में कुछ इनपुट है – MaatDeamon
एक्सेस-कंट्रोल-अनुमति-उत्पत्ति सामने वाला पता है। आप सभी को अनुमति देने के लिए "*" सेट कर सकते हैं। – raonirenosto