我是 Spring 的新手,出于某种原因,我在 Spring 和 Tomcat 中开发动态 Web 应用程序
我没有使用 Spring Security。我想阻止用户访问登录页面
已经在 session 中。这是我的代码:
@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
哪里那个 session 是从哪里来的?
(出于这个原因,在我的其他部分,我在浏览器中得到:“该网页有一个重定向循环”)
任何人都可以建议我在不使用 Spring Security 的情况下实现我的目标的方法吗?
这对我来说现在非常需要......
谢谢...
最佳答案
您可以使用 session 变量。
HttpSession ses=request.getSession(false);
if(ses.getAttribute("sessionVar") == null)
//show login page
else
// don't show login page
设置 session 变量:
HttpSession ses=request.getSession();
ses.setAttribute("sessionVar","someValueToShowSession");
关于java - 如何在不使用spring security的情况下限制登录用户在spring中访问登录页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10753069/