我的登陆的方法:
@RequestMapping(value = "/dologin.req")
public String doLogin(HttpServletRequest request, Model model) {
String msg = "";
String userName = request.getParameter("userName");
String password = request.getParameter("password");
UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
token.setRememberMe(true);
Subject subject = SecurityUtils.getSubject();
try {
subject.login(token);
if (subject.isAuthenticated()) {
return "redirect:/home.req";
} else {
return "login";
}
} catch (IncorrectCredentialsException e) {
msg = "登录密码错误. Password for account " + token.getPrincipal() + " was incorrect.";
model.addAttribute("message", msg);
System.out.println(msg);
} catch (ExcessiveAttemptsException e) {
msg = "登录失败次数过多";
model.addAttribute("message", msg);
System.out.println(msg);
} catch (LockedAccountException e) {
msg = "帐号已被锁定. The account for username " + token.getPrincipal() + " was locked.";
model.addAttribute("message", msg);
System.out.println(msg);
} catch (DisabledAccountException e) {
msg = "帐号已被禁用. The account for username " + token.getPrincipal() + " was disabled.";
model.addAttribute("message", msg);
System.out.println(msg);
} catch (ExpiredCredentialsException e) {
msg = "帐号已过期. the account for username " + token.getPrincipal() + " was expired.";
model.addAttribute("message", msg);
System.out.println(msg);
} catch (UnknownAccountException e) {
msg = "帐号不存在. There is no user with username of " + token.getPrincipal();
model.addAttribute("message", msg);
System.out.println(msg);
} catch (UnauthorizedException e) {
msg = "您没有得到相应的授权!" + e.getMessage();
model.addAttribute("message", msg);
System.out.println(msg);
}
return "login";
}
登陆错误的时候:
我的springmvc下返回目录views下,的确没有login.jsp,应为login.jsp如图已经在web-app下。
那么如何设置登陆错误 跳回login.jsp下呢?
下面是我的shiro的配置文件:
<bean id = "shiroFilter" class = "org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- Shiro的核心安全接口,这个属性是必须的 -->
<property name = "securityManager" ref = "securityManager"></property>
<!-- 要求登录时的链接(登录页面地址),非必须的属性,默认会自动寻找Web工程根目录下的"/login.jsp"页面 -->
<property name = "loginUrl" value = "/login.jsp" />
<!-- 登录成功后要跳转的连接(本例中此属性用不到,因为登录成功后的处理逻辑在LoginController里硬编码) -->
<!--<property name="successUrl" value="/home.jsp" ></property>-->
<!-- 用户访问未对其授权的资源时,所显示的连接 -->
<property name = "unauthorizedUrl" value = "/login.jsp"></property>
<property name = "filterChainDefinitions">
<value>
/dologin.req = anon
/**=authc
/WEB-INF/views/=authc
</value>
</property>
</bean>
你的login.jsp是jsp文件,是需要服务器去解析再返回的,不能直接这样给jsp的路径。
在WEB-INF目录下的jsp如果不是通过服务器解析直接去请求是访问不到的。
根据你的问题所说你的view目录下面是没有login.jsp,这可能是你的路径错了。
你的shiro配置路径中,其中loginUrl应该配置为服务器的请求路径而不是jsp文件路径。例如填
/login
,你的请求路径你需要写在springmvc中,例如到login
这个方法时,则