<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<!-- 放行 -->
<http pattern="/favicon.ico" security="none" />
<http pattern="/shoplogin.html" security="none" />
<http pattern="/css/**" security="none" />
<http pattern="/img/**" security="none" />
<http pattern="/js/**" security="none" />
<http pattern="/plugins/**" security="none" />
<http pattern="/register.html" security="none" />
<http pattern="/seller/add.do" security="none" />
<!-- 拦截条件(设定访问身份) use-expressions 是否使用默认表达式,默认为true,就是一种格式,false时直接写'ROLE_USER' -->
<http use-expressions="true" >
<!--指定intercept-url访问所有的权限,通行角色为ROLE_USER,intercept-url是浏览器的地址栏的路径-->
<intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
<!--
login-page —— 登录页面
default-target-url —— 登录成功跳转的页面
always-use-default-target —— 是否一直使用这个登录成功的页面
authentication-failure-url —— 登录失败跳转的页面
注意:路径前面一定要加 / !!!!!!!!!!!!
-->
<form-login login-page="/shoplogin.html"
default-target-url="/admin/index.html" always-use-default-target="true"
authentication-failure-url="/shoplogin.html"
/>
<!--退出时直接访问 "/logout" -->
<logout />
<!--关闭跨域检查-->
<csrf disabled="true" />
<!--做页面嵌套-->
<headers>
<!--
frame是html页面 用来划分多个页面在同一页面上所占比例的标签
deny:表示该页面不允许在 frame 中展示,即便是在相同域名的页面中嵌套也不允许。
sameorigin:表示该页面可以在相同域名页面的 frame 中展示。
allow-from uri:表示该页面可以在指定来源的 frame 中展示。
-->
<frame-options policy="SAMEORIGIN" />
</headers>
</http>
<!-- 身份认证管理器 -->
<authentication-manager>
<authentication-provider user-service-ref="userDetailService">
<!--对登录密码进行加密操作 为了和数据库中已加密的password匹配 没加密时不设置这个就好了-->
<password-encoder ref="bCryptPasswordEncoder"/>
</authentication-provider>
</authentication-manager>
<!--配置自定义 用户认证服务类 做登录验证的-->
<beans:bean class="com.offcn.sellergoods.controller.UserDetailsServiceImpl" id="userDetailService">
<!--自定义类中属性赋值-->
<!--<beans:property name="sellerService" ref="sellerService"/>-->
</beans:bean>
<!--做远程服务调用,到dubbo上寻找这个SellerService-->
<dubbo:application name="dongyimai_seller_web"/>
<dubbo:registry address="zookeeper://192.168.188.128:2181"/>
<dubbo:annotation package="com.offcn.sellergoods.service.SellerService"/>
<!--下面这种是扫描接口 然后通过set和get方法注入到 自定义用户认证服务类中 上面是直接扫描接口中标签-->
<!--<dubbo:reference interface="com.offcn.sellergoods.service.SellerService" id="sellerService"/>-->
<!--在注册时配置了密码加密 那么在登录验证时也要做加密操作-->
<beans:bean class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" id="bCryptPasswordEncoder"/>
</beans:beans>
做登录控制的类
package com.offcn.sellergoods.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.offcn.pojo.TbSeller;
import com.offcn.sellergoods.service.SellerService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.util.ObjectUtils;
import java.util.ArrayList;
public class UserDetailsServiceImpl implements UserDetailsService {
@Reference
private SellerService sellerService;
/*set和get方法 不使用@Reference远程调用注解时赋值用的
public SellerService getSellerService() {
return sellerService;
}
public void setSellerService(SellerService sellerService) {
this.sellerService = sellerService;
}*/
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//设置一个授权身份
ArrayList<GrantedAuthority> grantedAuthorities = new ArrayList<>();
//往集合里添加 xml配置文件里设置的放行身份
grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_USER"));
//通过username获取数据库中的一整条数据(主要是密码)
TbSeller user = sellerService.findOne(username);
//查询到的这个user不为空,并且状态的值为1(即审核通过的)
if (!ObjectUtils.isEmpty(user) && "1".equals(user.getStatus())){
//这里的User是实现了UserDetails接口的,参数分别为:用户名、密码、授权的身份
//账号和密码的匹配与否是交给springSecurity去处理的
return new User(username,user.getPassword(),grantedAuthorities);
}
return null;
}
}