22分布式电商项目 - 商家系统登录与安全控制

本文涉及的产品
密钥管理服务KMS,1000个密钥,100个凭据,1个月
简介: 22分布式电商项目 - 商家系统登录与安全控制

1.自定义认证类

1.pom.xml、web.xml 参照运营商管理后台

2.在 pinyougou-shop-web 创 建 com.pinyougou.service 包 , 包 下 创 建 类UserDetailsServiceImpl.java 实现 UserDetailsService 接口

package com.pyg.shop.service.impl;
import java.util.ArrayList;
import java.util.List;
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 com.pyg.manager.service.SellerService;
import com.pyg.pojo.TbSeller;
public class UserDetailsServiceImpl implements UserDetailsService {
  private SellerService sellerService;
  public SellerService getSellerService() {
    return sellerService;
  }
  public void setSellerService(SellerService sellerService) {
    this.sellerService = sellerService;
  }
  @Override
  public UserDetails loadUserByUsername(String username)
      throws UsernameNotFoundException {
    //定义角色封装集合
    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    //添加角色
    authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
    //调用商家服务对象,查询商家密码
    TbSeller seller = sellerService.findOne(username);
    return new User(username, seller.getPassword(), authorities);
  }
}

3.在 pinyougou-shop-web 的 spring 目录下创建 spring-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<bean:beans xmlns="http://www.springframework.org/schema/security"
  xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:bean="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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  <!-- 在权限认证之前访问资源需要放行 -->
  <http pattern="/shoplogin.html" security="none"></http>
  <http pattern="/error.html" security="none"></http>
  <http pattern="/register.html" security="none"></http>
  <http pattern="/css/**" security="none"></http>
  <http pattern="/js/**" security="none"></http>
  <http pattern="/img/**" security="none"></http>
  <http pattern="/plugins/**" security="none"></http>
  <!-- 商家入驻url -->
  <http pattern="/seller/add" security="none"></http>
  <!-- http安全控制规则 -->
  <http>
    <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
    <!-- 表单认证 -->
    <form-login login-page="/shoplogin.html"
      default-target-url="/admin/index.html" always-use-default-target="true"
      authentication-failure-url="/error.html" login-processing-url="/login" />
    <!-- 屏蔽跨域 -->
    <csrf disabled="true" />
    <!-- 配置ifram访问 -->
    <headers>
      <frame-options policy="SAMEORIGIN" />
    </headers>
    <!-- 推出登录配置 -->
    <!-- logout: spring security 安全框架自动生成推出地址:/logout -->
    <logout logout-success-url="/shoplogin.html" />
  </http>
  <!-- 引用dubbo 服务 -->
  <dubbo:application name="pinyougou-shop-web" />
  <dubbo:registry address="zookeeper://192.168.232.128:2181" />
  <!-- xml引入sellerService对象 -->
  <dubbo:reference id="sellerService"
    interface="com.pyg.manager.service.SellerService"></dubbo:reference>
  <!-- 配置认证管理器 -->
  <authentication-manager>
    <authentication-provider user-service-ref="userDetailsServiceImpl">
      <password-encoder ref="passwordEncoder"></password-encoder>
    </authentication-provider>
  </authentication-manager>
  <!-- 自定义认证类 -->
  <bean:bean id="userDetailsServiceImpl"
    class="com.pyg.shop.service.impl.UserDetailsServiceImpl">
    <bean:property name="sellerService" ref="sellerService"></bean:property>
  </bean:bean>
  <!-- 定义spring security安全加密算法对象 -->
  <bean:bean id="passwordEncoder"
    class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"></bean:bean>
</bean:beans>

2.认证类调用服务方法

修改 UserDetailsServiceImpl.java ,添加属性和 getter setter 方法 ,修改 loadUserByUsername方法。

public class UserDetailsServiceImpl implements UserDetailsService {
  private SellerService sellerService;
  public SellerService getSellerService() {
    return sellerService;
  }
  public void setSellerService(SellerService sellerService) {
    this.sellerService = sellerService;
  }
  @Override
  public UserDetails loadUserByUsername(String username)
      throws UsernameNotFoundException {
    //定义角色封装集合
    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    //添加角色
    authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
    //调用商家服务对象,查询商家密码
    TbSeller seller = sellerService.findOne(username);
    return new User(username, seller.getPassword(), authorities);
  }
}

修改 pinyougou-shop-web 的 spring-security.xml ,添加如下配置

<!-- 引用dubbo 服务 -->
  <dubbo:application name="pinyougou-shop-web" />
  <dubbo:registry address="zookeeper://192.168.232.128:2181" />
  <!-- xml引入sellerService对象 -->
  <dubbo:reference id="sellerService"
    interface="com.pyg.manager.service.SellerService"></dubbo:reference>
  <!-- 配置认证管理器 -->
  <authentication-manager>
    <authentication-provider user-service-ref="userDetailsServiceImpl">
      <password-encoder ref="passwordEncoder"></password-encoder>
    </authentication-provider>
  </authentication-manager>
  <!-- 自定义认证类 -->
  <bean:bean id="userDetailsServiceImpl"
    class="com.pyg.shop.service.impl.UserDetailsServiceImpl">
    <bean:property name="sellerService" ref="sellerService"></bean:property>
  </bean:bean>

3.密码加密

1.BCrypt 加密算法

用户表的密码通常使用 MD5 等不可逆算法加密后存储,为防止彩虹表破解更会先使用一个特定的字符串(如域名)加密,然后再使用一个随机的 salt(盐值)加密。 特定字符串是程序代码中固定的,salt 是每个密码单独随机,一般给用户表加一个字段单独存储,比较麻烦。 BCrypt 算法将 salt 随机并混入最终加密后的密码,验证时也无需单独提供之前的salt,从而无需单独处理 salt 问题。

2.商家入驻密码加密

商家申请入驻的密码要使用 BCrypt 算法进行加密存储,修改 SellerController.java 的 add 方法

/**
* 增加
* @param seller
* @return
*/
@RequestMapping("/add")
public Result add(@RequestBody TbSeller seller){
  //密码加密
  BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
  String password = passwordEncoder.encode(seller.getPassword());
  seller.setPassword(password);
  try {
    sellerService.add(seller);
    return new Result(true, "增加成功");
  } catch (Exception e) {
    e.printStackTrace();
    return new Result(false, "增加失败");
  }
}
3.加密配置

修改 pinyougou-shop-web 的 spring-security.xml ,添加如下配置

<bean:bean id="bcryptEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />

修改认证管理器的配置

<!-- 认证管理器 -->
<authentication-manager alias="authenticationManager"> 
   <authentication-provider user-service-ref='userDetailService'> 
     <password-encoder ref="bcryptEncoder"></password-encoder> 
   </authentication-provider> 
 </authentication-manager>



目录
相关文章
|
12天前
|
NoSQL Java Redis
面试官:项目中如何实现分布式锁?
面试官:项目中如何实现分布式锁?
39 6
面试官:项目中如何实现分布式锁?
|
1月前
|
存储 块存储
ceph分布式存储系统常见术语篇
关于Ceph分布式存储系统的常见术语解释和概述。
50 1
ceph分布式存储系统常见术语篇
|
2月前
|
Rust 安全 算法
揭秘Rust语言如何重塑区块链安全:打造坚不可摧的分布式账本新篇章!
【8月更文挑战第31天】自比特币诞生以来,区块链技术凭借其去中心化和不可篡改的特点备受关注。为了应对安全性挑战,Rust 语言凭借其内存安全特性逐渐成为区块链开发的优选。本文探讨了 Rust 如何助力区块链实现更安全的分布式账本。通过示例展示了 Rust 在避免内存泄漏、空指针引用及数据竞争等方面的优势,预示着 Rust 在高性能、高安全性需求的区块链应用中拥有广阔前景。
51 1
|
2月前
|
消息中间件 存储 监控
消息队列系统中的确认机制在分布式系统中如何实现?
消息队列系统中的确认机制在分布式系统中如何实现?
|
2月前
|
运维 安全 Cloud Native
核心系统转型问题之保障云原生分布式转型中的基础设施和应用层面如何解决
核心系统转型问题之保障云原生分布式转型中的基础设施和应用层面如何解决
|
2月前
|
监控 Cloud Native 容灾
核心系统转型问题之API网关在云原生分布式核心系统中的功能如何解决
核心系统转型问题之API网关在云原生分布式核心系统中的功能如何解决
|
2月前
|
运维 安全 Cloud Native
核心系统转型问题之分布式数据库和数据访问中间件协作如何解决
核心系统转型问题之分布式数据库和数据访问中间件协作如何解决
|
2月前
|
运维 Cloud Native 安全
核心系统转型问题之确保核心系统云原生分布式转型的安全可靠性如何解决
核心系统转型问题之确保核心系统云原生分布式转型的安全可靠性如何解决
|
2月前
|
机器学习/深度学习 分布式计算 PyTorch
构建可扩展的深度学习系统:PyTorch 与分布式计算
【8月更文第29天】随着数据量和模型复杂度的增加,单个GPU或CPU已无法满足大规模深度学习模型的训练需求。分布式计算提供了一种解决方案,能够有效地利用多台机器上的多个GPU进行并行训练,显著加快训练速度。本文将探讨如何使用PyTorch框架实现深度学习模型的分布式训练,并通过一个具体的示例展示整个过程。
54 0
|
2月前
|
运维 Cloud Native 容灾
核心系统转型问题之云原生分布式核心,业务敏捷该如何实现
核心系统转型问题之云原生分布式核心,业务敏捷该如何实现
下一篇
无影云桌面