2013-04-06 74 views
12

Tôi có dự án Spring MVC + Spring Security.Ngăn chặn chuyển hướng đăng nhập cho Spring Security

<http auto-config="true" access-denied-page="/security/accessDenied" use-expressions="true" disable-url-rewriting="true"> 

... 
<intercept-url pattern="/dashboard/myaccount/**" access="hasAnyRole('ROLE_PERSON', 'ROLE_DEALER')"/> 
... 

<form-login login-page="/security/login" authentication-failure-url="/security/login?error=true" 
       default-target-url="/security/success" username-parameter="email" 
       password-parameter="secret"/> 
<logout invalidate-session="true" logout-success-url="/index" logout-url="/security/logout"/> 

Nếu người dùng đi đến trang đăng nhập, nếu thành công sẽ được chuyển đến "/ security/thành công", nơi tôi làm công cụ hơn trong bộ điều khiển với các đối tượng session (kỷ lục userID, ... vv)

Vấn đề của tôi là khi người dùng GUEST truy cập/dashboard/myaccount (yêu cầu AUTH), anh ta đang được chuyển đến trang LOGIN (Tôi không muốn, tôi thích một trang 404 bị ném). Sau đó Spring Security không chuyển hướng đến/security/success. Thay vào đó được chuyển hướng đến/dashboard/myaccount.

Tôi muốn tìm cách tắt hoàn toàn chuyển hướng này sang trang đăng nhập trong trường hợp GUEST đang cố gắng truy cập trang AUTH.

Có cách nào để thực hiện việc này không?

Tnx

Trả lời

5

Tìm thấy này: luôn luôn sử dụng mặc định-target = "true"

tôi theo cách này, chức năng điều khiển của tôi là luôn luôn gọi sau khi bất kỳ đăng nhập.

11

Chúng tôi thêm một authenticationEntryPoint mới:

<http auto-config="true" access-denied-page="/security/accessDenied" use-expressions="true" 
     disable-url-rewriting="true" entry-point-ref="authenticationEntryPoint"/> 

<beans:bean id="authenticationEntryPoint" class="a.b.c..AuthenticationEntryPoint"> 
    <beans:constructor-arg name="loginUrl" value="/security/login"/> 
</beans:bean> 

public class AuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {  
    public AuthenticationEntryPoint(String loginUrl) { 
     super(loginUrl); 
    } 

    @Override 
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { 
     response.sendError(403, "Forbidden"); 
    } 
} 
+1

Không cần phải tạo riêng AuthenticationEntryPoint, sử dụng của bạn: org. springframework.security.web.authentication.Http403ForbiddenEntryPoint – unify

+0

@unify Tôi rất vui vì tôi đã xem xét nhận xét của bạn rất nhiều sạch hơn – ndrone

+0

Bạn có pl dễ dàng hãy xem câu hỏi của tôi quá https://stackoverflow.com/q/47849246/2556354 – madz

2

Trong cấu hình chú thích trong SpringSecurity 4 bạn có thể làm:

public class SecurityConfig extends WebSecurityConfigurerAdapter { 

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    // .... 
    http.exceptionHandling().authenticationEntryPoint(new AuthenticationEntryPoint() { 

     @Override 
     public void commence(HttpServletRequest request, HttpServletResponse response, 
       AuthenticationException authException) throws IOException, ServletException { 
      if (authException != null) { 
       response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); 
       response.getWriter().print("Unauthorizated...."); 
      } 
     } 
    }); 
    // .... 
} 

}

+0

Tôi tìm thấy lời khuyên này giúp tôi trong một tình huống khác. Cảm ơn bạn – madz

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