Java 如何在不使用spring安全性的情况下限制登录用户访问spring中的登录页面?
我是Spring新手,在Spring和Tomcat中开发动态web应用程序,出于某种原因Java 如何在不使用spring安全性的情况下限制登录用户访问spring中的登录页面?,java,spring,session,login,Java,Spring,Session,Login,我是Spring新手,在Spring和Tomcat中开发动态web应用程序,出于某种原因 我没有使用Spring安全性。我想阻止用户访问登录页面 已在会话中。以下是我的代码: @Controller @RequestMapping("/login") @SessionAttributes("userAuthenticator") public class LoginController { @RequestMapping(method = RequestMethod.G
我没有使用Spring安全性。我想阻止用户访问登录页面
已在会话中。以下是我的代码:
@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.
}
现在,请忘记else部分。当我在浏览器中输入URL时第一次:..../AccountCreation/login.htm
控制台输出:
Current Session: null
This means a new user wants to access the login page
看起来非常正常,因为新用户正在访问该页面(登录页面也会出现)
但是当我重新输入URL时,甚至刷新控制台输出的页面:
Current Session: org.apache.catalina.session.StandardSessionFacade@511e0a
该课程来自哪里?(由于这个原因,在我的else部分中,我在浏览器中看到了:“网页有一个重定向循环”) 有人能给我推荐一种不使用Spring Security实现目标的方法吗?
这是我现在非常需要的
谢谢…一旦用户对页面发出第一个请求,会话就会启动。因此,第二次访问将始终提供有效的会话 如果您想检查他是否是经过身份验证的用户,那么您需要在会话中添加一些标志/值。类似于userId的东西。你可以查一下 如果在会话->有效用户->重定向中设置了userId
如果用户ID未在会话->未登录用户->允许登录页面中设置。您可以使用会话变量
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");
这只是预期的行为。当您第一次登录时,将创建会话并在浏览器上存储jsessionid cookie。除非您使用session.invalidate()使会话无效或会话因会话超时而过期,否则HttpSession将可用于该浏览器窗口
如果您想通过键入登录页面url来限制已登录的用户查看登录页面,则创建一个拦截器,并检查用户是否已登录该页面。如果用户已经登录,则将其重定向到主页…否则允许他转到登录页。我发现,当用户向登录页发出请求时,无论是否存在
@SessionAttributes()
,Spring都会启动会话。因此,唯一的方法是按照您所说的存储特定会话值。