Day04-Spring Security框架入门——商家登录

5.商家系统登录与安全控制

5.1需求分析

完成商家系统登陆与安全控制,商家账号来自数据库,并实现密码加密

5.2自定义认证类

  1. pom.xml、web.xml  、login.html  参照运营商管理后台
  2. 在pinyougou-shop-web创建com.pinyougou.service包,包下创建类UserDetailsServiceImpl.java 实现UserDetailsService接口
  3. 认证类调用服务方法

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

    package com.pinyougou.shop.service;
    
    import com.pinyougou.pojo.TbSeller;
    import com.pinyougou.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 java.util.ArrayList;
    import java.util.List;
    
    /**
     * @Auther: jun
     * @Date: 2018/8/21 0021 09:23
     * @Description: 认证类实现
     */
    public class UserDetailsServiceImpl implements UserDetailsService {
    
        private SellerService sellerService;
    
        public void setSellerService(SellerService sellerService) {
            this.sellerService = sellerService;
        }
    
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    
            //构建角色列表
            List granthAuths = new ArrayList<> ();
            granthAuths.add (new SimpleGrantedAuthority ("ROLE_SELLER"));
            //查询通过id也就是username查询商家
            TbSeller seller = sellerService.findOne (username);
            //判断商家是否存在
            if (seller!=null){//存在
                //判断商家状态是否是审核通过的
                if (seller.getStatus ().equals ("1")){
                    //是 返回用户
                    return new User (username,seller.getPassword (),granthAuths);
                }else {
                    return null;
                }
            }
            return null;
        }
    }
    
  4. 在pinyougou-shop-web的spring目录下创建spring-security.xml
    
        
        
        
        
        
        
        
        
        
        
        
            
            
            
            
            
            
    
            
                
            
            
            
        
        
        
            
            
                
                
            
        
        
        
            
        
    
        
        
        
        
        
        
    
        
        

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

          

        <dubbo:application name="pinyougou-shop-web" />

        <dubbo:registry address="zookeeper://192.168.25.129:2181"/>

        <dubbo:reference id="sellerService"  interface="com.pinyougou.sellergoods.service.SellerService" >

        dubbo:reference>

        <beans:bean id="userDetailService" class="com.pinyougou.service.UserDetailsServiceImpl">

            <beans:property name="sellerService" ref="sellerService">bean:property>

        beans:bean>

    经过上述修改后,在登陆页输入用户名和密码与数据库一致即可登陆 。

 

你可能感兴趣的:(品优购)