2013-03-12 38 views
12

Tôi đang cố gắng tìm hiểu, cách bảo mật mùa xuân hoạt động, vì vậy tôi đã tải xuống một số dự án mẫu và sau đó tôi đã cố gắng triển khai giải pháp đó cho dự án của mình. Nhưng khi tôi cố gắng đăng nhập, tôi gặp lỗi 404 và trong thanh địa chỉ tôi có http://localhost:8080/fit/j_spring_security_check. Tôi đã cố gắng để xem xét các câu hỏi tương tự ở đây, nhưng tôi đã không thể nhận ra, làm thế nào để áp dụng nó vào dự án của tôi. Tôi thực sự biết ơn, nếu ai đó, người có nhiều kinh nghiệm hơn, có thể giúp tôi.Spring 3 Security j_spring_security_check

cấu trúc Ứng dụng của tôi trông như thế này:

enter image description here

applicationContext.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:context="http://www.springframework.org/schema/context" 
    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/context http://www.springframework.org/schema/context/spring-context-3.0.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="cz.cvut.fit"/> 

<import resource="classpath:applicationContext-security.xml"/> 

</beans> 

applicationContext-web.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:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:security="http://www.springframework.org/schema/security" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.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="cz.cvut.fit" /> 

<mvc:annotation-driven /> 

<security:global-method-security jsr250-annotations="enabled" 
           proxy-target-class="true"/> 
</beans> 

applicationContext-security.xml:

<beans xmlns:security="http://www.springframework.org/schema/security" 
    xmlns="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.0.xsd 
      http://www.springframework.org/schema/security 
      http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 

<security:http pattern="/css/**" security="none"/> 
<security:http pattern="/views/login.jsp*" security="none"/> 
<security:http pattern="/views/denied.jsp" security="none"/> 

<security:http auto-config="true" access-denied-page="/denied.jsp" servlet-api-provision="false"> 
    <security:intercept-url pattern="/views/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY"/> 
    <security:intercept-url pattern="/views/edit/**" access="ROLE_EDIT"/> 
    <security:intercept-url pattern="/views/admin/**" access="ROLE_ADMIN"/> 
    <security:intercept-url pattern="/**" access="ROLE_USER"/> 
    <security:form-login login-page="/views/login.jsp" authentication-failure-url="/denied.jsp" 
         default-target-url="/home.jsp"/> 
    <security:logout/> 
</security:http> 

<security:authentication-manager> 
    <security:authentication-provider> 
     <security:user-service> 
      <security:user name="adam" password="adampassword" authorities="ROLE_USER"/> 
      <security:user name="jane" password="janepassword" authorities="ROLE_USER, ROLE_ADMIN"/> 
      <security:user name="sue" password="suepassword" authorities="ROLE_USER, ROLE_EDIT"/> 
     </security:user-service> 
    </security:authentication-provider> 
</security:authentication-manager> 

</beans> 
+7

'j_spring_security_check' là một Servlet nơi xác thực thực tế được thực hiện và bạn phải ánh xạ hành động của mẫu đăng nhập của bạn để Servlet này. Bạn đang làm điều này trên trang đăng nhập của bạn - '

...
'? – Lion

+1

Vui lòng hiển thị tệp web.xml của bạn./j_spring_security_check URL phải được xử lý bằng bộ lọc springSecurityFilterChain. –

+0

Vâng, tôi ... Nhưng tôi không có đầu mối, phải làm gì tiếp theo, để làm cho nó hoạt động tốt. : -/ – Dworza

Trả lời

9

Bạn đang cố gắng xác thực một uri dựa trên đường dẫn ngữ cảnh hiện tại của trang web. có thể sử dụng lib thẻ JSTL để đảm bảo bạn dễ dàng tạo các url chính xác dựa trên ngữ cảnh của ứng dụng. Bạn có thể làm điều này bằng cách sử dụng thư viện thẻ nếu bạn muốn làm cho nó được triển khai nhanh chóng. Để thực hiện việc này, bạn có thể thêm thư viện thẻ jstl vào đầu của jsp:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

Sau đó, bạn có thể sử dụng thông tin sau để đăng lên servlet đăng nhập.

<form action="<c:url value="/j_spring_security_check"></c:url>" method="post" role="form"> 

Điều này đảm bảo bạn luôn được đăng lên < your_application_context>/j_spring_security_check.

tham khảo cho JSTL: http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/c/url.html

Các vấn đề liên quan