2011-05-11 30 views
8

मैं वसंत सुरक्षा 3.0.5 और परिवर्तन लॉग इन URL अनुकूलित करने के/लॉगिन/j_spring_security_check के बजाय होना चाहते हैं।स्प्रिंग सुरक्षा कस्टम फ़िल्टर

मैं क्या करने की जरूरत, "/" निर्देशिका के लिए प्रवेश की अनुमति देने के लिए और सुरक्षित करने के लिए "/admin/report.html" पृष्ठ है। यह बाद

public class MyFilter extends AbstractAuthenticationProcessingFilter { 
    private static final String DEFAULT_FILTER_PROCESSES_URL = "/login"; 
    private static final String POST = "POST"; 
    public static final String SPRING_SECURITY_FORM_USERNAME_KEY = "j_username"; 
    public static final String SPRING_SECURITY_FORM_PASSWORD_KEY = "j_password"; 
    public static final String SPRING_SECURITY_LAST_USERNAME_KEY = "SPRING_SECURITY_LAST_USERNAME"; 

    private String usernameParameter = SPRING_SECURITY_FORM_USERNAME_KEY; 
    private String passwordParameter = SPRING_SECURITY_FORM_PASSWORD_KEY; 

    protected MyFilter() { 
     super(DEFAULT_FILTER_PROCESSES_URL); 
    } 

    @Override 
    public Authentication attemptAuthentication(HttpServletRequest request, 
               HttpServletResponse response) 
          throws AuthenticationException, IOException, ServletException { 
     String username = obtainUsername(request); 
     String password = obtainPassword(request); 

     if (username == null) { 
      username = ""; 
     } 

     if (password == null) { 
      password = ""; 
     } 

     username = username.trim(); 
     UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password); 
     HttpSession session = request.getSession(false); 
     if (session != null || getAllowSessionCreation()) { 
      request.getSession().setAttribute(SPRING_SECURITY_LAST_USERNAME_KEY, TextEscapeUtils.escapeEntities(username)); 
     } 
     setDetails(request, authRequest); 

     return this.getAuthenticationManager().authenticate(authRequest); 
    } 

    protected void setDetails(HttpServletRequest request, UsernamePasswordAuthenticationToken authRequest) { 
     authRequest.setDetails(authenticationDetailsSource.buildDetails(request)); 
    } 

    @Override 
    public void doFilter(ServletRequest req, ServletResponse res, 
         FilterChain chain) throws IOException, ServletException { 
     final HttpServletRequest request = (HttpServletRequest) req; 
     final HttpServletResponse response = (HttpServletResponse) res; 
     if (request.getMethod().equals(POST)) { 
      // If the incoming request is a POST, then we send it up 
      // to the AbstractAuthenticationProcessingFilter. 
      super.doFilter(request, response, chain); 
     } else { 
      // If it's a GET, we ignore this request and send it 
      // to the next filter in the chain. In this case, that 
      // pretty much means the request will hit the /login 
      // controller which will process the request to show the 
      // login page. 
      chain.doFilter(request, response); 
     } 
    } 

    protected String obtainUsername(HttpServletRequest request) { 
     return request.getParameter(usernameParameter); 
    } 

    protected String obtainPassword(HttpServletRequest request) { 
     return request.getParameter(passwordParameter); 
    } 
} 

मैं एक्सएमएल में निम्न परिवर्तन बना रही हूँ

<security:http auto-config="true"> 
     <!--<session-management session-fixation-protection="none"/>--> 
     <security:custom-filter ref="myFilter" before="FORM_LOGIN_FILTER"/> 
     <security:intercept-url pattern="/admin/login.jsp*" filters="none"/> 
     <security:intercept-url pattern="/admin/report.html" access="ROLE_ADMIN"/> 
     <security:form-login login-page="/admin/login.jsp" login-processing-url="/login" always-use-default-target="true"/> 
     <security:logout logout-url="/logout" logout-success-url="/login.jsp" invalidate-session="true"/> 
    </security:http> 
<security:authentication-manager alias="authenticationManager"> 
    <security:authentication-provider> 
    <security:password-encoder hash="md5" /> 
    <security:user-service> 
    <!-- peter/opal --> 
     <security:user name="peter" password="22b5c9accc6e1ba628cedc63a72d57f8" authorities="ROLE_ADMIN" /> 
    </security:user-service> 
    </security:authentication-provider> 
</security:authentication-manager> 
<bean id="myFilter" class="com.vanilla.springMVC.controllers.MyFilter"> 
<property name="authenticationManager" ref="authenticationManager"/> 
</bean> 

और फिर मैं JSP है:

सभी सबसे पहले मैं अपने खुद के ट्यूटोरियल और स्प्रिंग सुरक्षा स्रोत कोड का उपयोग कर फिल्टर बनाने के मेरे कोड के साथ।

<form action="../login" method="post"> 
    <label for="j_username">Username</label> 
    <input type="text" name="j_username" id="j_username" /> 
    <br/> 
    <label for="j_password">Password</label> 
    <input type="password" name="j_password" id="j_password"/> 
    <br/> 
    <input type='checkbox' name='_spring_security_remember_me'/> Remember me on this computer. 
    <br/> 
    <input type="submit" value="Login"/> 
</form> 

/admin/report.html पर नेविगेट करने का प्रयास करते समय मुझे लॉगिन पृष्ठ पर रीडायरेक्ट किया जाता है। लेकिन साख जमा करने के बाद मैं हो रही है:

HTTP Status 404 - /SpringMVC/login/ 

type Status report 

message /SpringMVC/login/ 

description The requested resource (/SpringMVC/login/) is not available. 

ऐसा लगता है कि मैं विन्यास में समस्या है, लेकिन मैं इसे बाहर यह है कि क्या के कारण समझ नहीं सकता। क्या आप मदद कर सकते हैं?

+0

** 'action =" ../ login "' **? – lschin

+0

यह सही है, ../login क्योंकि othrwise मुझे/व्यवस्थापक/लॉगिन मिलेगा और मुझे मूल फ़ोल्डर की आवश्यकता है। –

+0

आपके आवेदन की रूट '/ SpringMVC' है? '/ SpringMVC/लॉगिन /' सही/उपलब्ध होना चाहिए? – lschin

उत्तर

0

मुझे लगता है कि @Ischin प्रपत्र कार्रवाई यूआरएल के बारे में चिंता सही है। पूरा पथ डालने का प्रयास करें और देखें कि क्या यह काम करता है। यदि ऐसा होता है, तो आप यह पता लगाने के लिए वहां से काम कर सकते हैं कि मेल नहीं खा रहा है।

केवल दूसरी बात मैं जाँच करने के लिए के बारे में सोच सकते हैं फिल्टर मानचित्रण आप में web.xml है। चूंकि आप लॉगिन पेज पर क्लिक कर रहे हैं, इसलिए आपने यह सेट अप किया है, लेकिन मैं जांचता हूं कि आप केवल विशिष्ट एक्सटेंशन, आदि के साथ यूआरएल को अवरुद्ध नहीं कर रहे हैं।

इसके अलावा, अगर आप अनुरोध चाहते हैं तो एक फीई के रूप में (एक बार जब -प्रवेश हमेशा उपयोग-default-लक्ष्य = "true": प्रवेश फार्म उपयोगकर्ता) इस मामले में सुरक्षित संसाधन (/admin/report.html पर जाने के लिए) तो आप प्रपत्र को निकाल देना चाहिए प्रमाणित करता है। इस ध्वज को सही पर सेट करने से अनुरोध हमेशा डिफ़ॉल्ट लक्ष्य यूआरएल पर जा सकता है, जो आम तौर पर आप जो चाहते हैं वह नहीं है। UsernamePasswordAuthenticationFilter की defaultTargetUrl संपत्ति को

मानचित्र: spring security docs से। सेट नहीं होने पर डिफ़ॉल्ट मान "/" (आवेदन जड़) है। लॉग इन करने के बाद पर लॉग इन करने के बाद प्रदान किए जाने के बाद उन्हें लॉगिन करने के लिए कहा नहीं गया था, जब उन्हें मूल रूप से अनुरोध किए गए URL पर ले जाया जाएगा।

+0

डाउनवोट के बारे में अधिक जानकारी के लिए कृपया नीचे दिए गए उत्तर को देखें –

10

मैं इस पर लगभग 12 महीने देर से हूं, लेकिन वसंत सुरक्षा फॉर्म लॉगिन के लॉगिन यूआरएल को अनुकूलित करने के लिए आपको अपना खुद का फ़िल्टर बनाने की आवश्यकता नहीं है। फॉर्म-लॉगिन टैग के गुणों में से एक आपको कस्टम यूआरएल सेट करने की अनुमति देता है। वास्तव में, आप फॉर्म-लॉगिन टैग के गुणों का उपयोग करके डिफ़ॉल्ट j_username और j_password फ़ील्ड नाम भी बदल सकते हैं। यहां एक उदाहरण दिया गया है:

<form-login login-page="/login" login-processing-url="/login.do" default-target-url="/" always-use-default-target="true" authentication-failure-url="/login?error=1" username-parameter="username" password-parameter="password"/> 
संबंधित मुद्दे