用户注册并登录后,打开以下页面: https://myApp.by/assets/inputmask/jquery.inputmask.min.js
为什么第一次登录时不是到主页而是到 /assets/inputmask/jquery.inputmask.min.js
据我了解,此页面仅打开一次。我真的很想了解到底发生了什么,以免将来遇到问题。
我最后一次查看注册表已经是两个多月前了..所以我不能说问题是什么时候出现的。
登录.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title th:text=#{login.title}></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="0"/>
<link href="http://fonts.googleapis.com/css?family=Roboto:300,400,500,700" rel="stylesheet" type="text/css"></link>
<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet" type="text/css"></link>
<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet" type="text/css"></link>
<link th:href="@{/css/bootstrap-material-design.css}" rel="stylesheet" type="text/css"></link>
<link th:href="@{/css/ripples.min.css}" rel="stylesheet" type="text/css"></link>
<link th:href="@{/css/style.css}" rel="stylesheet" type="text/css"></link>
</head>
<body>
<div id="wrap">
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" th:text=#{title}></a>
</div>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" th:utext=#{language}><span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="?lang=en">EN</a></li>
<li><a href="?lang=ru">RU</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<div class="container" id="login">
<div class = "login">
<div class="well">
<form th:action="@{/login}" method="post">
<span class="login-title" th:text="#{login.title}"></span>
<div class="text-center" th:if="${param.error}">
<span class="help-block" style="display: block; color: red" th:text="#{error.login}"></span>
</div>
<div class="form-group">
<input class="form-control" type="text" id="username" name="username" autofocus="autofocus" th:attr="placeholder=#{user.placeholder}"/>
</div>
<div class="form-group">
<input class="form-control" type="password" id="password" name="password" th:attr="placeholder=#{pass.placeholder}"/>
</div>
<div class="text-center">
<input type="submit" class="btn btn-primary" th:attr="value=#{login.action}"/>
</div>
<span class="login-title" th:text="#{login.reg.question}"/></span>
<div class="text-center">
<a class="btn btn-primary" th:href="@{/register}" th:text="#{register.title}"></a>
</div>
</form>
</div>
</div>
</div>
</div>
<footer class="navbar navbar-fixed-bottom footer">
<div class="container">
<div class="footer-copyright">
<a href="https://www.myApp.by">myApp.BY</a>
</div>
</div>
</footer>
<script th:src="@{/js/jquery.min.js}"></script>
<script type="text/javascript" th:src="@{/js/bootstrap.min.js}"></script>
<script type="text/javascript" th:src="@{/js/moment-with-locales.min.js}"></script>
<script type="text/javascript" th:src="@{/js/material.min.js}"></script>
<script type="text/javascript" th:src="@{/js/ripples.min.js}"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#locales").change(function () {
var selectedOption = $('#locales').val();
if (selectedOption != ''){
window.location.replace('international?lang=' + selectedOption);
}
});
});
</script>
</body>
</html>
和控制器
@Controller
public class LoginController {
@Autowired
UserRepository userRepository;
@RequestMapping("/login")
public String login() {
return "login";
}
@RequestMapping("/")
public String index(Locale locale, Model model, RedirectAttributes attrs) {
model.addAttribute("locale", locale);
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username;
if (principal instanceof UserDetails) {
username = ((UserDetails)principal).getUsername();
} else {
username = principal.toString();
}
User loginedUser = userRepository.findByLogin(username);
System.out.println("User logined" + loginedUser.toString());
if(loginedUser.isBanned()) {
return "redirect:/welcomeBanned";
}
if(SecurityContextHolder.getContext().getAuthentication().getAuthorities().contains(Role.DEFAULT)) {
return "redirect:/welcome";
}
if(SecurityContextHolder.getContext().getAuthentication().getAuthorities().contains(Role.USER)) {
return "redirect:/index";
}
if(loginedUser.getRoles().isEmpty()) {
return "redirect:/welcome";
}
return null;
}
我认为问题可能出在你的 if 语句中。尝试使用重定向编辑 if null 语句,以便将它们发送到主页等位置。如果不满足任何条件,这应该会停止程序将用户发送到随机 URL。
所以而不是
return null;
有(或选择您想发送到的地方)
return "redirect:/login";