無法訪問頁面,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.靜態(tài)資源默認servlet配置?(1)加入對靜態(tài)資源的處理: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以字節(jié)為單位–>--> <!--??????<property?name="maxUploadSize"?value="20971520"></property>--> <!--??????<!–最大內存,value以字節(jié)為單位–>--> <!--??????<property?name="maxInMemorySize"?value="20971520"></property>--> <!--???</bean>--> ???<!--創(chuàng)建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表示需要經過認證之后才可以訪問相應數(shù)據(jù),anon表示不需要認證,直接可以訪問--> ????????????<!--這個過濾器鏈是有順序的,從上往下匹配,匹配到之后就直接返回了,所以匹配到login.jsp不需要認證,/*需要認證,所以放在下面--> ????????????/login.jsp?=?anon ????????????/subLogin?=?anon ????????????/*?=?authc ?????????</value> ??????</property> ???</bean> ???<!--創(chuàng)建securityManager對象--> ???<bean?class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"?id="securityManager"> ??????<property?name="realm"?ref="myRealm"/> ???</bean> ???<!--創(chuàng)建自定義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"/><!--設置加密次數(shù)為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?"登錄成功"; ????} }
2020-04-07
在web.xml中添加
試試