2013-05-17 10 views
8

मुझे स्प्रिंग के साथ मूल स्वामित्व का उपयोग करते समय 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> 

उत्तर

11

बस अपने तरीके से पाया:

इस

मेरी स्प्रिंग सुरक्षा एक्सएमएल है

सबसे पहले, मैं वास्तव में याद नहीं है कारण है कि मैं इस लाइन को यहाँ रखा है, लेकिन यह ऊपर खिलवाड़ किया गया था मेरी कोड:

<security:http-basic /> 

दूसरा, इस सवाल का जवाब मुझे रास्ता दिखाने: Handle unauthorized error message for Basic Authentication in Spring Security। एक्सेस-कंट्रोल-अनुमति-उत्पत्ति चीज़ भेजने के लिए मुझे कस्टम प्रमाणीकरण प्रविष्टि बिंदु बनाना था।

तो यह अब मेरी कोड है:

<?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="/api/admin/**" /> 
     <security:intercept-url pattern="/medico/**" /> 
     <!-- <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="test.com" /> 
    </bean> --> 


    <bean id="authenticationEntryPoint" 
     class="com.test.util.PlainTextBasicAuthenticationEntryPoint"> 
     <property name="realmName" value="test.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> 
package com.test.util; 

import java.io.IOException; 
import java.io.PrintWriter; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import org.springframework.security.core.AuthenticationException; 
import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint; 

public class PlainTextBasicAuthenticationEntryPoint extends 
     BasicAuthenticationEntryPoint { 

     @Override 
     public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { 
      response.addHeader("Access-Control-Allow-Origin", "null"); 
      response.addHeader("WWW-Authenticate", "Basic realm=\"" + getRealmName() + "\""); 
      response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); 
      PrintWriter writer = response.getWriter(); 
      writer.println("HTTP Status " + HttpServletResponse.SC_UNAUTHORIZED + " - " + authException.getMessage()); 
     } 

} 

मेरे http अब प्रतिक्रिया:

OPTIONS http://localhost:8080/test/customer/name 200 (OK) jquery-1.8.2.min.js:2 
XMLHttpRequest cannot load http://localhost:8080/test/customer/name. Origin null is  not allowed by Access-Control-Allow-Origin. 

:

HTTP/1.1 401 Unauthorized 
Server: Apache-Coyote/1.1 
Access-Control-Allow-Origin: null 
WWW-Authenticate: Basic realm="test.com" 
Content-Length: 35 
Date: Mon, 20 May 2013 20:05:03 GMT 

HTTP Status 401 - Bad credentials 
परिवर्तन से पहले

, मैं इस त्रुटि संदेश मिला और अब उम्मीद के रूप में मुझे यह मिलता है:

OPTIONS http://localhost:8080/test/customer/name 200 (OK) jquery-1.8.2.min.js:2 
POST http://localhost:8080/test/customer/name 401 (Unauthorized) 
+0

धन्यवाद आदमी। आपके समाधान में मदद की। –

+0

हाय, मैं आपको निम्नलिखित से पूछना चाहता हूं। मैं जानना चाहता हूं कि क्रॉस अनुरोध + मूल प्राधिकरण + वसंत सुरक्षा सक्षम करने के लिए कुछ विशिष्ट है या नहीं। अब तक मैंने टॉमकैट फ़िल्टर (मैंने हेडर को प्राधिकरण जोड़ा है) सेट करने के लिए आवश्यक सबकुछ किया है, मैं AJAX में अपना अनुरोध करता हूं जैसा कि यह होना चाहिए, यह देखते हुए कि यह प्रमाणीकरण के बिना काम करता है, इसलिए मुझे आश्चर्य है कि वसंत सुरक्षा कोई समस्या नहीं पैदा कर रही है या नहीं ..... हो सकता है कि आपके पास इसके बारे में कुछ इनपुट है – MaatDeamon

+0

एक्सेस-कंट्रोल-अनुमति-उत्पत्ति सामने वाला पता है। आप सभी को अनुमति देने के लिए "*" सेट कर सकते हैं। – raonirenosto

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