使用shiro框架进行用户验证

主要思想

首先配置spring-shiro.xml文件,调用安全管理器securityManager去进行工作,让数据库中的数据能够添加到数据源relam,进行验证,

1.使用shiro前先添加依赖

<properties>
	<shiro.version>1.5.2</shiro.version>
</properties>
<!--shiro 核心-->
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-core</artifactId>
    <version>${shiro.version}</version>
</dependency>
<!--shiro 的 Web 模块-->
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-web</artifactId>
    <version>${shiro.version}</version>
</dependency>
<!--shiro 和 Spring 集成-->
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring</artifactId>
    <version>${shiro.version}</version>
</dependency>
<!--shiro 底层使用的 ehcache 缓存-->
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-ehcache</artifactId>
    <version>${shiro.version}</version>
</dependency>
<!--shiro 依赖的日志包-->
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
</dependency>
<!--shiro 依赖的工具包-->
<dependency>
    <groupId>commons-collections</groupId>
    <artifactId>commons-collections</artifactId>
    <version>3.2.1</version>
</dependency>
<!--Freemarker 的 shiro 标签库-->
<dependency>
    <groupId>net.mingsoft</groupId>
    <artifactId>shiro-freemarker-tags</artifactId>
    <version>1.0.1</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-all</artifactId>
        </exclusion>
    </exclusions>
</dependency>

2.配置代理过滤器

    <filter-name>shiroFilter</filter-name>
    <filter-class>
        org.springframework.web.filter.DelegatingFilterProxy
    </filter-class>
</filter>
<filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

这里使用了一个代理过滤器DelegatingFilterProxy,因为真正的shiroFilter需要注入很多复杂的对象,而web.xml中只能配置字符串或数字的参数,是不能满足的,因此我们会把shiroFilter交给 Spring 进行管理,通过spring的xml文件来配置。 使用DelegatingFilterProxy代理过滤器后,但浏览器发送请求过来,被代理过滤器拦截到后,代理过滤器会自动从 spring 容器中找filter-name所配置相同名称的bean,来实现真正的业务。

配置spring-shiro.xml文件

1.首先需要导入realm数据源的位置在这里插入代码片`

<context:component-scan base-package="cn.wolfcode.rbac.shiro.realm"></context:component-scan>`

2.配置shiro过滤器

<bean id="shiroFilter"
      class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> 
    <!--引用指定的安全管理器-->
    <property name="securityManager" ref="securityManager"/>
    <!--shiro默认的登录地址是/login.jsp 现在要指定我们自己的登录页面地址-->
    <property name="loginUrl" value="/login.html"/>
    <!--路径对应的规则-->
    <property name="filterChainDefinitions">
        <value>
            /login.do=anon
            /css/**=anon
            /js/**=anon
            /**=authc
        </value>
    </property>
</bean>

3.配置安全管理器

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="crmRealm"></property>
    </bean>

ref="crmRealm"在realm类的上边使用注解componet中添加

4.配置自定义realm

protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String username=(String)authenticationToken.getPrincipal();//获取用户名
        System.out.println(username);
        //通过service层调用selectByUsername去查找数据库是否有这个名字
        Employee employee=employeeService.selectByUsername(username);
        if(employee==null){
            return null;
        }
        SimpleAuthenticationInfo ret = new SimpleAuthenticationInfo(employee,employee.getPassword(),this.getName());
        //返回值是查到employee的所有信息
        return ret;
    }

5.登录功能

try {
            UsernamePasswordToken token = new  UsernamePasswordToken(username, password);
            //获取用户名和密码
            //下边这句话进行验证,如果没有异常,进行成功显示
            //如果抛异常,会根据不同异常进行抛出
            SecurityUtils.getSubject().login(token);
            return ResultBean.success();
        } catch (UnknownAccountException e) {
            return ResultBean.fail("账号不存在");
        } catch (IncorrectCredentialsException e) {
            return ResultBean.fail( "密码错误");
        } catch (Exception e) { e.printStackTrace();
            return ResultBean.fail( "登录异常,请联系管理员");
        }

登录功能获取输入的用户名和密码,用户名验证是在shiro框架进行验证,密码是在根据ssm框架功能进行验证

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值