<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns="http://www.springframework.org/schema/security"
xmlns:beans="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.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
<http pattern="/login.html" security="none"/>
<http pattern="/css/**" security="none"/>
<http pattern="/img/**" security="none"/>
<http pattern="/js/**" security="none"/>
<http pattern="/plugins/**" security="none"/>
<!-- use-expressions:设置是否启动SpEL表达式,默认值是true。 -->
<http use-expressions="false">
<!--
配置SpringSecurity的拦截路径(拦截规则)
* pattern:配置拦截规则。 /* 代表的是根路径下的所有资源(不包含子路径) /**代表的是根路径下所有的资源(包含子路径)
* access:设置角色 角色命名 ROLE_角色名称 如: ROLE_USER
-->
<intercept-url pattern="/**" access="ROLE_ADMIN"/>
<!--
开启表单验证
username-parameter="username"
password-parameter="password"
login-page :登录页面名称 以 / 开始
default-target-url :登录成功后跳转的页面
login-processing-url:提交的路径的设置 默认值"/login" 可以修改
lways-use-default-target="true" :登录跳转成功之后,必须欢迎界面.
-->
<form-login
login-page="/login.html"
default-target-url="/admin/index.html"
always-use-default-target="true"
authentication-failure-url="/login.html"/>
<!-- 不使用csrf的校验 -->
<csrf disabled="true"/>
<!--
配置框架页面不拦截
页面:一个页面
框架页面:二个以上页面的组合页面 如一个index.html 和user.html
同源策略:SAMEORIGIN
用户登录时,两个页面都不拦截,
用户没有登录时,两个页面都会拦截
-->
<headers>
<frame-options policy="SAMEORIGIN"/>
</headers>
<!-- 注销的配置 -->
<logout logout-url="/logout" logout-success-url="/login.html" />
</http>
<!-- 配置认证管理器 -->
<authentication-manager>
<!-- 认证的提供者 -->
<authentication-provider>
<user-service>
<user name="admin" password="123456" authorities="ROLE_ADMIN"/>
<user name="wc" password="123456" authorities="ROLE_ADMIN"/>
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
2.回显用户登录姓名
在jsp页面中有两种方法,因为spring_security框架会默认将登录信息,存储到sesssion域中,然后从sessionScope中取出即可
在目前最流行的html界面中,我们可以页面加载的时候进行初始化页面,调用控制层方法.然后在控制层中将登陆信息取出进行回显.
//1.直接从session中取出来,默认的健名是SPRING_SECURITY_SESSION
SecurityContext securityContext = (SecurityContext) session.getAttribute("SPRING_SECURITY_SESSION");
String name = securityContext.getAuthentication().getName();
//2.SpringSecurity有一个小助手
final String name1 = SecurityContextHolder.getContext().getAuthentication().getName();