![](/img/trans.png)
[英]How to allow guest to access home page without login in Spring Security
[英]How to restrict a logged in user to access the login page in spring without using spring security?
我是Spring的新手,在Spring和Tomcat中开发动态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.