这不是 duplicate
,因为预期副本包含错误java.lang.NoSuchMethodError
,而我收到的错误java.lang.IllegalStateException
是不同的条款
在spring-security
工作时我收到错误
SEVERE: Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
//堆栈跟踪的其余部分
Sep 20, 2016 4:06:34 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler [http-nio-8080]
Sep 20, 2016 4:06:34 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler [ajp-nio-8009]
Sep 20, 2016 4:06:34 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 7520 ms
代码如下
的web.xml
<!-- Servlet mapping -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- context parameters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/security-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Filter mappings -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
安全-context.xml中
<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:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.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-3.2.xsd">
<security:http auto-config="true">
<security:intercept-url pattern="/add" access="ROLE_ADMIN" />
<security:form-login login-page="/login"
default-target-url="/add" authentication-failure-url="/loginfailed" />
<security:logout logout-success-url="/logout" />
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="admin" password="admin"
authorities="ROLE_ADMIN" />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans>
的DispatcherServlet-servlet.xml中
<mvc:annotation-driven />
<context:component-scan base-package="com" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="com/resources/message" />
</bean>
</beans>
注意所有这些文件都直接位于/WEB-INF/
现在由于我的配置一切正常,为什么我会收到错误,因为我会做更多的配置,因此我会使用多个配置文件。
感谢任何帮助,请帮助:)
从this更改<context-parms>
阅读解决方案后,未解决错误
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/security-context.xml,/WEB-INF/dispatcherServlet-servlet.xml</param-value>
</context-param>
这是代码的另一部分
DomainController
@Controller
public class DomainController {
@Autowired
private DomainRepositiry repostiry;
@RequestMapping("/")
public String getHomePage(Model model) {
model.addAttribute("domains", repostiry.getList());
return "indexPage";
}
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String signUp(Model model) {
Domain domain = new Domain();
model.addAttribute("domain", domain);
return "home";
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String showResult(@ModelAttribute("domain") Domain domain, BindingResult result) {
String[] supressedFeilds = result.getSuppressedFields();
if (supressedFeilds.length > 0)
throw new RuntimeException("Attempting to bind disallowed feilds ");
repostiry.addToList(domain);
return "redirect:/";
}
@RequestMapping("/detail")
public String getInfoByName(@RequestParam String firstName, Model model) {
model.addAttribute("domain", repostiry.getDomainByFirstName(firstName));
return "detail";
}
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.setDisallowedFields("dateOfBirth");
}
}
的LoginController
@Controller
public class LoginContoller {
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login() {
return "login";
}
@RequestMapping(value = "/loginfailed", method = RequestMethod.GET)
public String loginFailed(Model model) {
model.addAttribute("error", "true");
return "login";
}
@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String logOut(Model model) {
return "login";
}
}
登录页面 登录 请输入您的凭据以登录 的的
<c:if test="${not empty error}">
<b><U><spring:message
code="AbstractUserDetailsAuthenticationProvider.badCredentials" /></U></b>
</c:if>
</h1>
<form action='<c:url value="/j_spring_security_check"></c:url>'
method="post">
<h3>
User Name : <br> <input type="text" name=j_username>
<hr>
Password : <br> <input type="text" name="j_password">
<hr>
</h3>
<input type="submit" value="login">
</form>
</body>
</html>
用户信息页面
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Basic Information</title>
</head>
<body>
<c:forEach var="domain" items="${domains}">
<p>First Name : ${domain.firstName}</p>
<br>
<p>Last Name : ${domain.lastName}</p>
<br>
<p>Number : ${domain.number}</p>
<br>
<p>Date Of Birth : ${domain.dateOfBirth}</p>
<br>
<HR>
<A
href='<spring:url value="/detail?firstName=${domain.firstName}"></spring:url>'>TO
VIEW DETAILS CLICK HERE</A>
<HR>
</c:forEach>
<p>
To add more information <a href='<spring:url value="/add" />'><h2>Click
here</h2></a>
</p>
</body>
</html>
注意:此代码不会抛出任何caused by
异常,而是顺利启动服务器,这就是我得到error 404
的原因,我正在尝试解决此问题,因此感谢任何帮助
答案 0 :(得分:0)
您在配置位置
中缺少dispatcherServlet
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/dispatcherServlet-servlet.xml,
/WEB-INF/security-context.xml
</param-value>
</context-param>
更新1: 在web.xml中更改为
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcherServlet-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
更新2:
在向Spring-security.xml
发出的 3.1
架构声明中,您使用的是3.2
,因此将其更改为3.2或删除版本< / strong>,无需提及,它会自动接受。
<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:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.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.xsd">
更新3:将 web.xml 更改为
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- context parameters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/security-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Filter mappings -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
<强>的dispatcherServlet.xml 强>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sec="http://www.springframework.org/schema/security"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="com/resources/message" />
</bean>
</beans>
安全-context.xml中强>
<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:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.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.xsd">
<security:http auto-config="true">
<security:intercept-url pattern="/add" access="ROLE_ADMIN" />
<security:form-login login-page="/login"
default-target-url="/add" authentication-failure-url="/loginfailed" />
<security:logout logout-success-url="/logout" />
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="admin" password="admin"
authorities="ROLE_ADMIN" />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans>
答案 1 :(得分:0)
在处理完这个error
之后,我发现有jar
版本冲突实际上我使用的是spring4 jars
和spring 3 security jar
个文件,而且Prasanna Kumar表示他的配置工作正常,我不确定为什么,但如果我使用context.xsd
代替context.n.m.xsd
,则会给我错误我的春季版本必须为3.1或以上
简短的故事 - 最终配置,这是有效的
<?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:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.1.xsd">
<security:http auto-config="true">
<security:intercept-url pattern="/"
access="hasRole('ROLE_ADMIN')" />
<security:form-login login-page="/login"
default-target-url="/" authentication-failure-url="/loginfailed" />
<security:logout logout-success-url="/logout" />
<security:csrf disabled="true" />
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="Admin" authorities="ROLE_ADMIN"
password="admin" />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans>
以及其他信息 我发现了一些常见错误,这是他们的修复
1)错误Could not verify the provided CSRF token because your session was not found.
在<security:csrf disabled="true" />
<security-http>
2)错误java.lang.IllegalArgumentException: Failed to evaluate expression 'ROLE_ADMIN'
使用 <security:intercept-url pattern="/" access="hasRole('ROLE_ADMIN')" />
,您无需在<security:authentication-manager>
代码
希望有所帮助