zoukankan      html  css  js  c++  java
  • Spring Security从后台数据库查询实现登陆控制

    Spring Security框架是一个控制登陆的框架,通过配置文件获取后台的用户名及密码,进行比较进行登陆判断

    使用步骤

      1、导入依赖

    复制代码
    <!-- 身份验证 -->
                <dependency>
                    <groupId>org.springframework.security</groupId>
                    <artifactId>spring-security-web</artifactId>
                    <version>4.1.0.RELEASE</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework.security</groupId>
                    <artifactId>spring-security-config</artifactId>
                    <version>4.1.0.RELEASE</version>
                </dependency>
    复制代码

      2、web.xml文件中加载spring-security.xml文件

    复制代码
     <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring/spring-security.xml</param-value>
          </context-param>
        <listener>
          <listener-class>
            org.springframework.web.context.ContextLoaderListener
          </listener-class>
        </listener>
    
        <filter>
          <filter-name>springSecurityFilterChain</filter-name>
          <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        </filter>
          <filter-mapping>
            <filter-name>springSecurityFilterChain</filter-name>
            <url-pattern>/*</url-pattern>
          </filter-mapping>
    复制代码

      3、前端页面

       

      4、后台代码中创建一个实体类实现 UserDetailsService接口,实现其中的方法

    复制代码
    //用户登录的认证类
    public class UserDetailsServiceImpl  implements UserDetailsService {
      //service层是获取后台数据库中的用户名信息
        private SellerService sellerService;
        //set方法注入一个
        public void setSellerService(SellerService sellerService) {
            this.sellerService = sellerService;
        }
        //通过用户名去拿数据库中对象,
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            System.out.println("经过了imp方法");
    //        username是得到前端表单的用户名
            //哪些用户有登陆权限,
            List<GrantedAuthority> grant = new ArrayList<>();
            //添加用户的角色信息,能够去使用
            grant.add(new SimpleGrantedAuthority("ROLE_SELLER"));
            //username是控制框架规定的,其实就是实体类的seller_id
            Seller seller = sellerService.findOne(username);
    //        若果不存在此用户
            if (seller !=null && "1".equals(seller.getStatus())){
                //只有当审核通过的商家才能登陆,返回的user是你输入的用户名,密码,角色,
                    return new User(username,seller.getPassword(),grant);
            }else {
                return null;
            }
        }
    复制代码

      5、spring-security
      里面的细则,我上一篇博客有细说,大家可以回顾一下

    复制代码
    <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"
        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 
        
        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="/*.html" security="none"></http>
        <http pattern="/css/**" security="none"></http>
        <http pattern="/img/**" security="none"></http>
        <http pattern="/js/**" security="none"></http>
        <http pattern="/plugins/**" security="none"></http>
        <!--<http pattern="/seller/add.do" security="none"></http>-->
       <!-- 页面拦截规则 -->
        <http use-expressions="false">
            <intercept-url pattern="/**" access="ROLE_SELLER" />
            <form-login login-page="/shoplogin.html"  default-target-url="/admin/index.html" authentication-failure-url="/shoplogin.html" always-use-default-target="true"/>
            <csrf disabled="true"/>
            <headers>
                <frame-options policy="SAMEORIGIN"/> //角色信息
            </headers>
            <logout/>
        </http>
        <!-- 认证管理器 -->
        <authentication-manager>
            <authentication-provider user-service-ref="userDetailService">
            </authentication-provider>
        </authentication-manager>
        <!-- 应用dubbo-->
        <dubbo:application name="youlexuan-shop" />
        <dubbo:registry address="zookeeper://192.168.200.128:2181"/>
        <dubbo:reference id="sellerService"  interface="com.ghh.core.service.SellerService" >
        </dubbo:reference>
     
    <!--自定义的配置类-->
      <beans:bean id="userDetailService" class="com.ghh.shop.service.UserDetailsServiceImpl"> 
        <beans:property name="sellerService" ref="sellerService"/> //ref引用dubbo中的sellerService
      </beans:bean>
    </beans:beans>
    复制代码

     自定义配置类中指向自定义的UserDetailsServiceImpl类中,将后台返回的用户名,密码,角色信息,进行控制判断

  • 相关阅读:
    OSI参考模型(转)
    H3C交换机配置常用命令(转)
    H3C交换机配置学习随笔
    [Swust OJ 247]--皇帝的新衣(组合数+Lucas定理)
    [Swust OJ 1084]--Mzx0821月赛系列之情书(双线程dp)
    [Swust OJ 404]--最小代价树(动态规划)
    [Swust OJ 610]--吉祥数
    [Swust OJ 137]--波浪数(hash+波浪数构造)
    [Swust OJ 566]--开N方数(牛顿切线法解高次方程)
    [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)
  • 原文地址:https://www.cnblogs.com/wangju/p/11886977.html
Copyright © 2011-2022 走看看