代码已上传至Github
地址:https://github.com/ylw-github/pingyougou.git
本文实现用户中心单点登录
详细步骤如下:
1)pom.xml 引入 springSecurity、cas 客户端和 springSecurity Cas 整合包依赖
2)web.xml 添加 spring-security 过滤器(参照参照 casclient_demo3)设置首页为home-index.html
<welcome-file-list>
<welcome-file>home-index.html</welcome-file>
</welcome-file-list>
3)构建 UserDetailsServiceImpl.java
4)添加 spring-security.xml(参照casclient_demo3),并做以下修改配置匿名访问资源
<!-- 匿名访问资源 --> <http pattern="/css/**" security="none"></http> <http pattern="/js/**" security="none"></http>
<http pattern="/image/**" security="none"></http>
<http pattern="/plugins/**" security="none"></http>
<http pattern="/register.html" security="none"></http>
<http pattern="/user/add.do" security="none"></http>
<http pattern="/user/sendCode.do" security="none"></http>
设置服务地址属性
<beans:bean id="serviceProperties"class="org.springframework.security.cas.ServiceProperties">
<beans:property name="service" value="http://localhost:9106/login/cas"/></beans:bean>
设置认证类
<beans:bean id="userDetailsService" class="com.pinyougou.user.service.UserDetailServiceImpl"/>
5)创建 LoginController.java
@RestController
@RequestMapping("/login")
public class LoginController {
@RequestMapping("/name")
public Map showName(){
String name =
SecurityContextHolder.getContext().getAuthentication().getName();
//得到登陆人账号
Map map=new HashMap<>();
map.put("loginName", name);
return map;
}
}
上面是登录的代码片段,现在看看退出登录代码
设置退出登录后的跳转地址
<beans:bean id="requestSingleLogoutFilter"
class="org.springframework.security.web.authentication.logout.LogoutFilter">
<beans:constructor-arg value="http://localhost:9100/cas/logout?service=http://localhost:9103"/>
........
</beans:bean>
退出登录后,跳转到网站首页
<span class="safe">
<a href="/logout/cas">退出登录 </a>
</span>