预期结果:
我想使用Spring Expression语言链接来使用上述条件。但是当我使用Spring Expression语言时,会发生错误。错误是“ java.lang.IllegalStateException:未找到WebApplicationContext:未注册ContextLoaderListener?”当我不使用Spring Expression语言时,不会发生错误。
控制台错误:
Jul 12, 2019 4:14:15 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [blog] in context with path [/educationBlog] threw exception [An exception occurred processing [/WEB-INF/view/index.jsp] at line [19]
16: <a href="${pageContext.request.contextPath}/createUser">Create User</a>
17:
18:
19: <sec:authorize access="!isAuthenticated()">
20: <p><a href="<c:url value='/login' />">Log in</a></p>
21: </sec:authorize>
22:
Stacktrace:] with root cause
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
at org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:84)
at org.springframework.security.taglibs.authz.AbstractAuthorizeTag.getExpressionHandler(AbstractAuthorizeTag.java:315)
at org.springframework.security.taglibs.authz.AbstractAuthorizeTag.authorizeUsingAccessExpression(AbstractAuthorizeTag.java:172)
at org.springframework.security.taglibs.authz.AbstractAuthorizeTag.authorize(AbstractAuthorizeTag.java:105)
at org.springframework.security.taglibs.authz.JspAuthorizeTag.doStartTag(JspAuthorizeTag.java:54)
at org.apache.jsp.WEB_002dINF.view.index_jsp._jspx_meth_sec_005fauthorize_005f0(index_jsp.java:200)
at org.apache.jsp.WEB_002dINF.view.index_jsp._jspService(index_jsp.java:152)
索引页:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/createUser">Create User</a>
<sec:authorize access="!isAuthenticated()">
<p><a href="<c:url value='/login' />">Log in</a></p>
</sec:authorize>
<sec:authorize access="isAuthenticated()">
<p><a href="<c:url value='/j_spring_security_logout' />">Log out</a></p>
</sec:authorize>
<sec:authorize access="hasRole('admin')">
<p><a href="<c:url value='/admin' />">Admin page</a></p>
</sec:authorize>
servlet:
<?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"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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-4.3.xsd">
<!-- <context:annotation-config></context:annotation-config> -->
<context:component-scan
base-package="com.web.spring.mvc.blog">
</context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="jspViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<jee:jndi-lookup jndi-name="jdbc/spring"
expected-type="javax.sql.DataSource" id="dataSource">
</jee:jndi-lookup>
<!-- Add support for reading web resources: css, images, js, etc ... -->
<mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename"
value="com.web.spring.mvc.blog.message.message">
</property>
</bean>
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="mithu" authorities="ROLE_ADMIN"
password="123456" />
<security:user name="sarker" authorities="ROLE_USER"
password="123456" />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
<security:http use-expressions="true">
<security:form-login login-page="/login"
authentication-failure-url="/login?error=true" />
<security:intercept-url pattern="/admin" access="hasRole('ROLE_ADMIN')" />
<security:intercept-url pattern="/login" access="permitAll" />
<security:intercept-url pattern="/loggedout" access="permitAll" />
<security:intercept-url pattern="/createUser" access="isAuthenticated()" />
<security:intercept-url pattern="/saveUser" access="isAuthenticated()" />
<security:intercept-url pattern="/" access="permitAll" />
<security:intercept-url pattern="/resources/**" access="permitAll" />
<security:intercept-url pattern="/**" access="denyAll" />
<security:logout logout-success-url="/loggedout" />
</security:http>
控制器:
@Controller
public class LoginController {
@RequestMapping(value="/login")
public String login() {
return "login";
}
@RequestMapping(value="/loggedout")
public String loggedout() {
return "loggedout";
}
@RequestMapping(value="/admin")
public String admin() {
return "admin";
}
}