出错记录:
1.Failed to evaluate expression ‘ROLE_USER’
解决办法:
在security.xml文件中 ,对有权限的用户都设置了role
<authentication-manager><!-- 权限提供者 -->
<authentication-provider>
<!-- 可提供登陆访问的用户 -->
<user-service>
<user name="haha" password="haha" authorities="ROLE_USER,ROLE_ADMIN" />
<user name="xixi" password="xixi" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
那么在拦截的时候security2.0 以前都是用的是 ,之后改了haveRole。注意一下。
2.注意:Spring Security 3默认关闭csrf,Spring Security 4默认启动了csrf。
3. security2.x的写法:对login.jsp放行,不用保护:
<intercept-url pattern="/login.jsp" filters="none" />
spring securit 3.X新的资源放行配置方式,不受保护的资源 -->
<http pattern="/login.jsp" security="none"/>
或者
<intercept-url pattern="/populate" access="permitAll" />
指明配置文件
spring Servlet文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->
<mvc:annotation-driven />
<!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->
<context:component-scan base-package="com.martina.securitytest" /> <!--这个包根据自己的项目来配置,我的是com.mvc.test-->
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/" p:suffix=".jsp" />
</beans>
其中:下图中需要注意
因为我们在controller中写的是:
另外,我们也实现了自定义登录界面的操作,只是修改http标签中的元素,并添加登录界面:
1.修改security.xml文件:
<http use-expressions="true" auto-config='true'>
<intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
<form-login login-page="/login.jsp"
default-target-url="/index.jsp" authentication-failure-url="/login.jsp?error=true" />
</http>
其中就多加 了一个form 配置节点,从命名可以看出login-page就是你的登陆页面,default-target-url是默认的登陆成功后的页 面,authentication-failure-url就是当你输入非法账号或者密码自动重定向到login.jsp.
login.jsp:
<form action="j_spring_security_check" method="post">
用户名<input type="text" name="j_username"/><br/>
密码<input type="password" name="j_password"/><br/>
<input type="submit" value="submit"/>
</form>
action="j_spring_security_check"必须这样写,否则拦截器拦截不到。登陆的字段j_username j_password必须这样写,否则你的登陆拦截器获取不到value。当然你也可以自定义,但前提是你必须继承 UsernamePasswordAuthenticationFilter 过滤器去重写一下,这里我们就不麻烦了,而且一般情况下我们不需要重写,就用它提供的即可。
记得给login.jsp放行:
<http pattern="/login.jsp" security="none"/>
附上源码:
spring Servlet文件见上文。
security.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<b:beans xmlns="http://www.springframework.org/schema/security"
xmlns:b="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<!-- spring securit 3.X新的资源放行配置方式,不受保护的资源 -->
<!--<http pattern="/login.jsp" security="none"/>-->
<http pattern="/**/*.jpg" security="none"/>
<http pattern="/**/*.png" security="none"/>
<http pattern="/**/*.gif" security="none"/>
<http pattern="/**/*.ico" security="none"/>
<http pattern="/**/*.css" security="none"/>
<http pattern="/**/*.js" security="none"/>
<http use-expressions="true" auto-config='true'>
<!--注意:Spring Security 3默认关闭csrf,Spring Security 4默认启动了csrf。-->
<!--security2.x的写法:对login.jsp放行,不用保护-->
<!--<intercept-url pattern="/login.jsp" filters="none" />-->
<intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
<!-- <form-login login-page="/login.jsp"
default-target-url="/index.jsp" authentication-failure-url="/login.jsp?error=true" />-->
</http>
<!-- 权限管理者 -->
<authentication-manager><!-- 权限提供者 -->
<authentication-provider>
<!-- 可提供登陆访问的用户 -->
<user-service>
<user name="haha" password="haha" authorities="ROLE_USER,ROLE_ADMIN" />
<user name="xixi" password="xixi" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
</b:beans>
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">
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 应用上下文配置文件 -->
<param-value>/WEB-INF/spring-servlet.xml,/WEB-INF/applicationContext-security.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spring securit start -->
<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>
<!-- 配置spring核心servlet -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- url-pattern配置为/,不带文件后缀,会造成其它静态文件(js,css等)不能访问。如配为*.do,则不影响静态文件的访问 -->
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
登录界面展示(默认登录):
转自:https://www.cnblogs.com/younggun/p/3434761.html