복붙노트

[SPRING] 스프링 보안 변경 spring_security_login 폼

SPRING

스프링 보안 변경 spring_security_login 폼

내가 봄 보안을 사용하고 기본 로그인 양식을 변경하는 방법을 궁금 해서요

나는 나의 새로운 형태의 위치를 ​​지적해야한다는 것을 알았다. 모든 로그인 예외 표시가있는 기존 기본 양식의 기존 기능을 유지하려고합니다. 그래서 나는 그것을 처음으로 재현하는 방법을 알아야합니다.

내 연구에서 나는 그것으로 건너 온다.

http://www.codercorp.com/blog/spring/security-spring/spring-security-login-logout-form.html

그를 코드에 대해 고맙다.

<%@ taglib prefix='c' uri='http://java.sun.com/jstl/core_rt' %>
<%@ page import="org.springframework.security.ui.AbstractProcessingFilter" %>
<%@ page import="org.springframework.security.ui.webapp.AuthenticationProcessingFilter" %>
<%@ page import="org.springframework.security.AuthenticationException" %>

<html>
  <head>
    <title>Login</title>
  </head>

  <body>
    <h1>Login</h1>

    <c:if test="${not empty param.login_error}">
      <font color="red">
        Your login attempt was not successful, try again.<br/><br/>
        Reason: <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}"/>.
      </font>
    </c:if>

    <form name="f" action="<c:url value='j_spring_security_check'/>" method="POST">
      <table>
        <tr><td>User:</td><td><input type='text' name='j_username' value='<c:if test="${not empty param.login_error}"><c:out value="${SPRING_SECURITY_LAST_USERNAME}"/></c:if>'/></td></tr>
        <tr><td>Password:</td><td><input type='password' name='j_password'></td></tr>
        <tr><td><input type="checkbox" name="_spring_security_remember_me"></td><td>Don't ask for my password for two weeks</td></tr>

        <tr><td colspan='2'><input name="submit" type="submit"></td></tr>
        <tr><td colspan='2'><input name="reset" type="reset"></td></tr>
      </table>
    </form>
  </body>
</html>

하지만 코드는 봄 보안을 위해 잘 작동하지 않는 것 같습니다. 3. 일부 라이브러리는 구식이며 하나씩 교체했습니다. 나는 이것을 아래에 바꾼다.

<%@ page import="org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter" %>
<%@ page import="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter" %>
<%@ page import="org.springframework.security.core.AuthenticationException" %>

로그인 오류를보고하는 것을 여전히 거부합니다. 내가 뭘 더해야하지?

해결법

  1. ==============================

    1.커스텀 로그인 페이지를 사용할 때, Spring Security는 로그인 에러의 결과로 로그인 페이지가 표시되었다고 자동으로 지정하지 않습니다. 일반적으로 authentication-failure-url에 매개 변수를 추가하여 수동으로해야합니다. login_error라는 매개 변수에 코드가 필요합니다.

    커스텀 로그인 페이지를 사용할 때, Spring Security는 로그인 에러의 결과로 로그인 페이지가 표시되었다고 자동으로 지정하지 않습니다. 일반적으로 authentication-failure-url에 매개 변수를 추가하여 수동으로해야합니다. login_error라는 매개 변수에 코드가 필요합니다.

    <form-login 
        login-page="/login.htm" 
        authentication-failure-url = "/login.htm?login_error=1" />
    
  2. ==============================

    2.Spring Security 애플리케이션 컨텍스트 XML 구성을 업데이트하는 것을 잊지 않았습니까? 내가 가지고있는 샘플은 다음과 같습니다.

    Spring Security 애플리케이션 컨텍스트 XML 구성을 업데이트하는 것을 잊지 않았습니까? 내가 가지고있는 샘플은 다음과 같습니다.

    <http auto-config="true">
      <intercept-url pattern="/login.htm" access="ROLE_ANONYMOUS,ROLE_USER" />
      <intercept-url pattern="/**" access="ROLE_USER" />
      <form-login login-page="/login.htm" default-target-url="/home.htm" />
      <logout logout-url="/logout.htm" logout-success-url="/home.htm"/>
    </http>
    

    따라서 form-login 태그를 사용하면 사용자 정의 로그인 페이지를 설정할 수 있습니다. 익명의 역할에 대한 로그인 페이지에 대한 액세스 권한을 부여하는 것을 기억해야합니다. 그렇지 않으면 아무도 로그인 페이지로 이동할 수 없습니다.

  3. from https://stackoverflow.com/questions/3139718/spring-security-changing-spring-security-login-form by cc-by-sa and MIT license