我试图通过myselft解决这个问题超过一周,但仍然不知道我错在哪里。我也阅读了所有有类似问题的帖子,但无法找到我案例的解决方案。
Web xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<!-- Servlet config begin -->
<servlet>
<servlet-name>vietjob</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>vietjob</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- end-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Begin load Spring Filter Chain config from servlet vietjob -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/vietjob_security.xml
</param-value>
</context-param>
<!-- end load -->
<!-- Begin Spring security config-->
<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>
<!-- End Spring security config -->
</web-app>
安全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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<import resource="vietjob-data.xml" />
<!-- Begin Spring security config -->
<sec:http auto-config="true" use-expressions="true">
<sec:intercept-url pattern="/secure/admin/**"
access="hasRole('ROLE_YLLAPITAJA')" />
<sec:intercept-url pattern="/secure/employee/*" access="hasRole('ROLE_EHDOKAS')" />
<sec:intercept-url pattern="/secure/hakemus/*" access="hasRole('ROLE_EHDOKAS')" />
<sec:intercept-url pattern="/secure/partner/*"
access="hasRole('ROLE_PARTNERI')" />
<sec:intercept-url pattern="/secure" access="isAuthenticated()" />
<sec:intercept-url pattern="/lomake.do" access="isAuthenticated()" />
<sec:intercept-url pattern="/*" access="permitAll" />
<sec:form-login login-page="/login.do"
default-target-url="/secure"
authentication-failure-url="/loginfail.do" />
<sec:logout logout-success-url="/logout.do" />
<sec:csrf disabled="true"/>
</sec:http>
<bean id="encoder"
class="org.springframework.security.crypto.password.StandardPasswordEncoder" />
<sec:authentication-manager>
<sec:authentication-provider>
<sec:password-encoder ref="encoder" />
<sec:jdbc-user-service data-source-ref="dataSource"
users-by-username-query="select kayttaja_tunnus, kayttaja_salasana_encrypted, kayttaja_enabled from kayttaja where kayttaja_tunnus=? and enabled = 1"
authorities-by-username-query="select k.kayttaja_tunnus, r.rooli from kayttaja k JOIN kayttaja_rooli kr ON (r.fk_kayttaja_id = kr_kayttaja_id) JOIN rooli r ON (r.rooli_id = kr.fk_rooli_id) WHERE k.kayttaja_tunnus = ?" />
</sec:authentication-provider>
</sec:authentication-manager>
<sec:global-method-security
pre-post-annotations="enabled" />
<!-- End Spring security config -->
</beans>
登录表单
<h3><spring:message code="login.kirjaudu.kayttaja" /></h3>
<hr>
<c:if test="${not empty loginerror}">
<p>Sisäänkirjautuminen epäonnistui. Käyttäjätunnus tai salasana on syötetty väärin.</p>
</c:if>
<c:if test="${not empty loggedout}">
<p>Uloskirjautuminen onnistui</p>
</c:if>
<spring:url value='j_spring_security_check' var="springcheck"></spring:url>
<form action='${springcheck }' class="form-horizontal" role="form" method="post">
<div class="form-group">
<label class="control-label col-sm-2"><spring:message code="login.tunnus.kayttaja"></spring:message>:</label>
<div class="col-sm-4">
<input type="text" class="form-control" name="j_username" placeholder="Enter username" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" ><spring:message code="login.salasana.kayttaja"></spring:message>:</label>
<div class="col-sm-4">
<input type="password" class="form-control" name="j_password" placeholder="Enter password" />
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-4">
<div class="checkbox">
<label><input type="checkbox"> <spring:message code="login.remember.kayttaja"></spring:message></label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-4">
<button type="submit" class="btn btn-default"><spring:message code="login.kirjaudu.kayttaja"/></button>
</div>
</div>
</form>
任何人都可以帮助我吗?我已经准备好放弃了:(。
答案 0 :(得分:1)
您有两种选择:
1.更改为版本3.2
或
2.在spring security config中,您在login-processing-url="/j_spring_security_check"
标记中添加<sec:form-login>
。
答案 1 :(得分:0)
默认值在Spring security 4中更改
https://docs.spring.io/spring-security/site/docs/4.2.0.RELEASE/reference/htmlsingle/#nsa-form-login-attributes
https://docs.spring.io/spring-security/site/docs/3.2.0.RELEASE/reference/htmlsingle/#nsa-form-login-attributes