簡體   English   中英

如何在不使用spring安全性的情況下限制登錄用戶在spring中訪問登錄頁面?

[英]How to restrict a logged in user to access the login page in spring without using spring security?

我是Spring的新手,在SpringTomcat中開發動態Web應用程序,出於某種原因
我沒有使用Spring Security 我想阻止用戶訪問登錄頁面的人
已經在session.Here是我的代碼:

@Controller  
@RequestMapping("/login")  
@SessionAttributes("userAuthenticator")  
public class LoginController {  

    @RequestMapping(method = RequestMethod.GET)  
    public String showLogin(ModelMap model, HttpServletRequest request) {  
    System.out.println("Current Session: " + request.getSession(false));  

    if (request.getSession(false) == null) {  
    System.out.println("This means a new user wants to access the login page");  
    return "login";  
    }  

    else{  
    //means the user is already in session.So move him to another page say display.jsp  
    //actually I have done here with some other checking like getUserName() from   
    the model "UserAuthenticator" and if its again null redirect to login page.  
    }  

For Now忘記了else部分。當我在瀏覽器中輸入URL時
第一次:.... / AccountCreation / login.htm
控制台輸出:

Current Session: null  
This means a new user wants to access the login page  

看起來絕對正常,因為新用戶正在訪問該頁面(也會出現登錄頁面)。

但是,當我重新輸入網址,甚至刷新控制台輸出來的頁面:

Current Session: org.apache.catalina.session.StandardSessionFacade@511e0a  

那次會議來自哪里
(出於這個原因,在我的其他部分,我得到:“我的瀏覽器中的網頁有一個重定向的循環”)

沒有人可以建議我在不使用Spring Security的情況下實現目標嗎?
這對我來說非常需要.....

謝謝...

您可以使用會話變量。

HttpSession ses=request.getSession(false);
if(ses.getAttribute("sessionVar") == null)
     //show login page
else
     // don't show login page

設置會話變量:

 HttpSession ses=request.getSession();
 ses.setAttribute("sessionVar","someValueToShowSession");

用戶向頁面發出第一個請求后,會話即開始。 因此,第二次訪問將始終提供有效的會話。

如果你想檢查他是否是經過身份驗證的用戶,那么你需要在會​​話中加入一些標志/值。 像userId之類的東西。 你可以查一下泰恩

如果在會話中設置userId - >有效用戶 - >重定向。

如果userId未在會話中設置 - >未登錄用戶 - >允許登錄頁面。

這只是預期的行為。 當您第一次登錄時,會創建會話並將jsessionid cookie存儲在瀏覽器中。 除非您使用session.invalidate()或會話因會話超時而使會話失效,否則HttpSession將可用於該瀏覽器窗口。

如果要限制已登錄的用戶通過鍵入登錄頁面URL來查看登錄頁面,則創建一個攔截器並檢查用戶是否已登錄。 如果用戶已經登錄,則將其重定向到主頁...否則允許他進入登錄頁面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM