无法访问页面,500错误
想问下这是怎么回事,明明跟着视频一步步敲代码的
相关配置和代码
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--shiro提供的Filter--> <filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!--将spring-dao.xml、spring-service.xml、spring-web.xml整合到一块--> <param-value>classpath:spring/spring-*.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 默认匹配所有请求 --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
spring-web.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 1.扫描web相关的bean --> <context:component-scan base-package="com.lgq.shirotest.controller" /> <!-- 2.开启SpringMVC注解模式 --> <mvc:annotation-driven /> <mvc:resources mapping="/*" location="/" /> <!-- 2.静态资源默认servlet配置 (1)加入对静态资源的处理:js,gif,png (2)允许使用"/"做整体映射 --> <!-- <mvc:resources mapping="/resources/**" location="/resources/" />--> <!-- <mvc:default-servlet-handler />--> <!-- 3.定义视图解析器 --> <!-- <bean id="viewResolver"--> <!-- class="org.springframework.web.servlet.view.InternalResourceViewResolver">--> <!-- <property name="prefix" value="/WEB-INF/jsp/"></property>--> <!-- <property name="suffix" value=".jsp"></property>--> <!-- </bean>--> <!-- <!– 文件上传解析器 –>--> <!-- <bean id="multipartResolver"--> <!-- class="org.springframework.web.multipart.commons.CommonsMultipartResolver">--> <!-- <property name="defaultEncoding" value="utf-8"></property>--> <!-- <!– 1024 * 1024 * 20 = 20M –>--> <!-- <!–文件上传的最大尺寸,value以字节为单位–>--> <!-- <property name="maxUploadSize" value="20971520"></property>--> <!-- <!–最大内存,value以字节为单位–>--> <!-- <property name="maxInMemorySize" value="20971520"></property>--> <!-- </bean>--> <!--创建shirtoFilter对象,给spring容器管理--> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <property name="loginUrl" value="login.jsp"/> <property name="unauthorizedUrl" value="403.jsp"/> <property name="filterChainDefinitions"> <!--shiro给我们内置了很多Filter,这里设置过滤器链--> <value> <!--authc表示需要经过认证之后才可以访问相应数据,anon表示不需要认证,直接可以访问--> <!--这个过滤器链是有顺序的,从上往下匹配,匹配到之后就直接返回了,所以匹配到login.jsp不需要认证,/*需要认证,所以放在下面--> /login.jsp = anon /subLogin = anon /* = authc </value> </property> </bean> <!--创建securityManager对象--> <bean class="org.apache.shiro.web.mgt.DefaultWebSecurityManager" id="securityManager"> <property name="realm" ref="myRealm"/> </bean> <!--创建自定义realm--> <bean class="com.lgq.shirotest.realm.CustomRealm" id="myRealm"> <property name="credentialsMatcher" ref="credentialsMatcher"/> </bean> <!--设置加密管理器对象--> <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher" id="credentialsMatcher"> <property name="hashAlgorithmName" value="md5"/><!--设置加密算法为md5--> <property name="hashIterations" value="1"/><!--设置加密次数为1--> </bean> <!-- 5.权限拦截器 --> </beans>
login.jsp:
<%-- Created by IntelliJ IDEA. User: 98184 Date: 2019/9/27 Time: 9:08 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登录</title> </head> <body> <form action="subLogin" method="post"> 用户名:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> <input type="submit" value="登录"> </form> </body> </html>
UserController.java:
package com.lgq.shirotest.controller; import com.lgq.shirotest.entity.User; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.subject.Subject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class UserController { @RequestMapping(value = "/subLogin", method = RequestMethod.POST,produces = "application/json;charset=utf-8") @ResponseBody public String subLogin(User user) { Subject subject= SecurityUtils.getSubject(); UsernamePasswordToken token=new UsernamePasswordToken(user.getUsername(),user.getPassword()); try { subject.login(token); } catch (AuthenticationException e) { return e.getMessage();//返回异常信息 } return "登录成功"; } }