Ich versuche zu erfahren, wie die Frühlingssicherheit funktioniert, also habe ich ein Beispielprojekt heruntergeladen und versucht, diese Lösung in meinem Projekt zu implementieren. Aber wenn ich versuche, mich anzumelden, erhalte ich 404
Fehler und in einer Adressleiste habe ich http://localhost:8080/fit/j_spring_security_check
. Ich habe versucht, ähnliche Fragen hier zu betrachten, konnte aber nicht erkennen, wie ich sie in meinem Projekt anwenden kann. Ich wäre wirklich dankbar, wenn mir jemand helfen könnte, der mehr Erfahrung hat.
Meine App-Struktur sieht folgendermaßen aus:
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>
Sie versuchen, die Überprüfung auf einen URI basierend auf dem aktuellen Kontextpfad der Webseite zu überprüfen. Mit dem JSTL-Tag lib kann sichergestellt werden, dass Sie auf einfache Weise die richtigen URLs basierend auf dem Kontext der Anwendung generieren. Sie können dies tun, indem Sie eine Tag-Bibliothek verwenden, wenn Sie diese schnell implementieren möchten. Dazu können Sie die jstl-Tag-Bibliothek oben in jsp hinzufügen:
<%@ taglib prefix="c" uri="http://Java.Sun.com/jsp/jstl/core" %>
Anschließend können Sie Folgendes zum Anmelden an das Login-Servlet verwenden.
<form action="<c:url value="/j_spring_security_check"></c:url>" method="post" role="form">
Dies stellt sicher, dass Sie immer auf <Ihr_Anwendungskontext>/j_spring_security_check posten.
referenz für jstl: http://docs.Oracle.com/javaee/5/jstl/1.1/docs/tlddocs/c/url.html
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Fügen Sie Ihre web.xml-Datei hinzu. Sie erstellen Bean Ihrer SpringSecurityFilterChain. Dann haben Sie eine Antwort