1.添加依赖

  <!-- shiro -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>

2.ApplicationContext-mvc.xml配置文件

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
"> <mvc:annotation-driven/>
<mvc:default-servlet-handler/> <context:component-scan base-package="com.wfd360.controller"/> <!-- 未认证或未授权时跳转必须在springmvc里面配,spring-shiro里的shirofilter配不生效 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<!--表示捕获的异常 -->
<prop key="org.apache.shiro.authz.UnauthorizedException">
<!--捕获该异常时跳转的路径 -->
/403
</prop>
<!--表示捕获的异常 -->
<prop key="org.apache.shiro.authz.UnauthenticatedException">
<!--捕获该异常时跳转的路径 -->
/403
</prop>
</props>
</property>
</bean> <!-- 配置SpringMVC的视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean> <import resource="classpath:spring-shiro.xml"/> </beans>

3.spring-shiro.xml配置文件

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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">
<!--开启shiro的注解-->
<bean id="advisorAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
<property name="proxyTargetClass" value="true"></property>
</bean>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"/>
<!--注入自定义的Realm-->
<bean id="customRealm" class="com.wfd360.shiro.CustomRealm"></bean>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="customRealm"></property>
</bean> <!--配置ShiroFilter-->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"></property>
<!--登入页面-->
<property name="loginUrl" value="/login.jsp"></property>
<!--登入成功页面-->
<property name="successUrl" value="/index.jsp"/>
<!-- <property name="filters">
<map>
&lt;!&ndash;退出过滤器&ndash;&gt;
<entry key="logout" value-ref="logoutFilter" />
</map>
</property>-->
<!--URL的拦截-->
<property name="filterChainDefinitions" >
<value>
/share = authc
/logout = logout
</value>
</property> </bean>
<!--自定义退出LogoutFilter-->
<!-- <bean id="logoutFilter" class="com.test.filter.SystemLogoutFilter">
<property name="redirectUrl" value="/login"/>
</bean>-->
</beans>

4.创建对象 CustomRealm.java

 package com.wfd360.shiro;

 import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection; import java.util.ArrayList;
import java.util.List; /**
* @author www.wfd360.com
* @date 2018/2/26 14:05
*/
public class CustomRealm extends AuthorizingRealm {
/**
* 授权
* @param principalCollection
* @return
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
String userName = (String) principalCollection.getPrimaryPrincipal();
List<String> permissionList=new ArrayList<String>();
permissionList.add("user:add");
permissionList.add("user:delete");
if (userName.equals("zhou")) {
permissionList.add("user:query");
}
SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
info.addStringPermissions(permissionList);
info.addRole("admin");
return info;
}
/**
* 认证
* @param authenticationToken
* @return
* @throws AuthenticationException
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
String userName = (String) authenticationToken.getPrincipal();
if ("".equals(userName)) {
return null;
}
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(userName,"123456",this.getName());
return info;
}
}

5.控制层ShiroController.java对象

 package com.wfd360.controller;

 import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; /**
* Created by Administrator on 2018/10/29.
*/
@Controller
public class ShiroController { @RequestMapping(value = "/loginData", method = RequestMethod.POST)
public String login(String userName, String passwd, Model model) {
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(userName, passwd);
try {
subject.login(token);
} catch (UnknownAccountException e) {
e.printStackTrace();
model.addAttribute("userName", "用户名错误!");
return "login";
} catch (IncorrectCredentialsException e) {
e.printStackTrace();
model.addAttribute("passwd", "密码错误");
return "login";
}
return "index";
} @RequestMapping(value = "/index2")
public String index() {
System.out.println("------index-------");
return "login";
}
}

6.登陆jsp页面login.jsp

 <%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/10/29
Time: 11:51
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登陆界面</title>
</head>
<body>
<h2>登陆界面</h2>
<form action="/loginData" method="post">
用户名:<input name="userName">
密码:<input name="passwd">
<input type="submit">
</form>
</body>
</html>

7.web.xml配置

 <!-- shiro 过滤器 start -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<!-- 设置true由servlet容器控制filter的生命周期 -->
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- shiro 过滤器 end -->

8.测试完成!

shiro 基于springmvc中做登陆功能的更多相关文章

  1. 在用easyui中做CRUD功能时,当删除一行或多行数据后再点击修改会提示你选中了多行,如何解决这个bug了?

    在用easyui中做CRUD功能时,当删除一行或多行数据后再点击修改会提示你选中了多行,如何解决这个bug了? 在删除成功后,加上这句话就可以了:$("#dg").datagrid ...

  2. springmvc中使用文件下载功能

    项目代码:https://github.com/PeiranZhang/springmvc-fileupload 使用文件下载步骤 对请求处理方法使用void或null作为返回类型,并在方法中添加Ht ...

  3. 【Spring】基于SpringMVC的图片验证码功能实现

    后台实现代码: ImgController.java 文件 package cn.shop.controller; import java.awt.Color; import java.awt.Fon ...

  4. 解决springmvc中文件下载功能中使用javax.servlet.ServletOutputStream out = response.getOutputStream();后运行出异常但结果正确的问题

    问题描述: 在springmvc中实现文件下载功能一般都会使用javax.servlet.ServletOutputStream out = response.getOutputStream();封装 ...

  5. 将 Shiro 作为应用的权限基础 二:基于SpringMVC实现的认证过程

    认证就是验证用户身份的过程.在认证过程中,用户需要提交实体信息(Principals)和凭据信息(Credentials)以检验用户是否合法.最常见的“实体/凭证”组合便是“用户名/密码”组合. 一. ...

  6. Spring集成shiro做登陆认证

    一.背景 其实很早的时候,就在项目中有使用到shiro做登陆认证,直到今天才又想起来这茬,自己抽空搭了一个spring+springmvc+mybatis和shiro进行集成的种子项目,当然里面还有很 ...

  7. 如何巧妙地在基于 TCP Socket 的应用中实现用户注册功能?

    通常,在基于TCP的应用中(比如我开源的GGTalk即时通信系统),当TCP连接建立之后,第一个请求就是登录请求,只有登录成功以后,服务器才会允许客户端进行其它性质的业务请求.但是,注册用户这个功能比 ...

  8. 详解SpringMVC中Controller的方法中参数的工作原理——基于maven

    转自:http://www.tuicool.com/articles/F7byQn 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:ht ...

  9. 如何在springMVC 中对REST服务使用mockmvc 做测试

    如何在springMVC 中对REST服务使用mockmvc 做测试 博客分类: java 基础 springMVCmockMVC单元测试  spring 集成测试中对mock 的集成实在是太棒了!但 ...

随机推荐

  1. leetcode 852. Peak Index in a Mountain Array

    Input: [0,1,0] Output: 1 Input: [0,2,1,0] Output: 1解: 比较数组中的i和i-1的大小,如果前一位大于后一位数字,前一位则是结果 let ans = ...

  2. linux中使用pip命令遇到的一些问题

    一 安装readline包之后python3.6导入模块异常退出 Type "help", "copyright", "credits" o ...

  3. Failed to execute request because the App-Domain could not be created. Error: 0x80070002 系统找不到指定的文件。

    360更新补丁后,网站就打不开aspx文件了,后来一查是framework2.0的KB2844285这个补丁引起的.把它卸载掉就ok了!          

  4. HBase中无法使用backspace删除

    转载自:Hbase命令行无法删除的问题 在HBase的shell命令行界面输入错误项按"退格键"删除,却怎么也删除不了: 解决办法: 第一步,修改SecureCRT的设置参数: 第 ...

  5. redis 主从配置和集群配置

    主从配置 |  集群配置 redis主从 主从配置原因: 1.到达读写分离,读的操作和写操作比例10 : 1读数据频繁,写数据次数少,这样可以配置1个master数据库用来写数据,配置多个slave从 ...

  6. 流程图工具Visual Paradigm for UML

  7. abc高级bash shell编程

    http://www.pythoner.com/122.html     abc高级bash shell编程

  8. hive数据倾斜原因以及解决办法

    何谓数据倾斜?数据倾斜指的是,并行处理的数据集 中,某一部分(如Spark的一个Partition)的数据显著多于其它部分,从而使得该部分的处理速度成为整个数据集处理的瓶颈. 表现为整体任务基本完成, ...

  9. 完全分布式hadoop2.5.0安装 VMware下虚拟机centos6.4安装1主两从hadoop

    请跟我走,从零开始搭建hadoop2.5.0环境.总览第一步:搭建三台能不用密码shh的虚拟机.第二步,装jdk,解压hadoop文件,配置环境变量和xml文件.第三步,复制克隆两个slave机器.调 ...

  10. Redis list 数据类型

    lpush()先进后出  //从头部加入元素   //栈      lrange 元素集合   0    -1 lpop  从list头部删除元素,并返回删除元素 rpush()先进先出 //从尾部加 ...