`
suichangkele
  • 浏览: 198184 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

shiro学习03-登录校验(1)

阅读更多

这一节说明shiro对登录模块的限制,这一节可能用到的接口有:SecurityManager,Realm,AuthenticationToken,AuthenticationInfo,PrincipalCollection,如果你对这些接口不熟悉的话,回到第一节先看看他们的大概介绍。

登录模块的大概流程很简单,用户提交用用名+密码,然后从数据库查找该用户,如果没有找到怎么样,找到了用户但是密码不对又怎么样。先将我搭建的shiro项目的pom.xml贴出,方便你的测试。在这个项目中我集成了spring+springMVC+shiro。

 

	<properties>
		<spring.version>4.1.6.RELEASE</spring.version>
		<junit.version>4.11</junit.version>
		<jackson.version>2.4.2</jackson.version>
  		<shiro.version>1.2.2</shiro.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>${jackson.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		 

		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-core</artifactId>
			<version>${shiro.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-web</artifactId>
			<version>${shiro.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-spring</artifactId>
			<version>${shiro.version}</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.6.6</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.12</version>
		</dependency> 
	</dependencies>

 我们在实际项目中对shiro的使用是集成在spring中使用的,不会用到官网上提到的shiro.ini,因为没有人会将权限 角色定义在一个文档中,我将spring的xml的配置和项目的web.xml的配置贴出来,以做到我们的项目是一样的。

web.xml

 

<web-app>
	<!-- needed for ContextLoaderListener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:beans.xml</param-value>
	</context-param>
 
	<filter>
		<filter-name>shiroFilter</filter-name>
		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
		<init-param>
			<param-name>targetFilterLifecycle</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>shiroFilter</filter-name>
		<url-pattern>*.do</url-pattern>
	</filter-mapping>
 
	<!-- Bootstraps the root web application context before servlet initialization -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
 
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
 
</web-app>

 spring的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:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	 http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-3.0.xsd ">

	<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
		<property name="securityManager" ref="securityManager" />
		<!-- override these for application-specific URLs if you like: <property 
			name="loginUrl" value="/login.jsp"/> <property name="successUrl" value="/home.jsp"/> 
			<property name="unauthorizedUrl" value="/unauthorized.jsp"/> -->
		<!-- The 'filters' property is not necessary since any declared javax.servlet.Filter 
			bean -->
		<!-- defined will be automatically acquired and available via its beanName 
			in chain -->
		<!-- definitions, but you can perform instance overrides or name aliases 
			here if you like: -->
		<!-- <property name="filters"> <util:map> <entry key="anAlias" value-ref="someFilter"/> 
			</util:map> </property> -->
		<!-- <property name="filterChainDefinitions"> <value> # some example chain 
			definitions: /admin/** = authc, roles[admin] /docs/** = authc, perms[document:read] 
			/** = authc # more URL-to-FilterChain definitions here </value> </property> -->
	</bean>
	<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />

	<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
		<!-- Single realm app. If you have multiple realms, use the 'realms' property 
			instead. -->
		<property name="realm" ref="myRealm" />
		<!-- By default the servlet container sessions will be used. Uncomment 
			this line to use shiro's native sessions (see the JavaDoc for more): -->
		<!-- <property name="sessionMode" value="native"/> -->
	</bean>
	<bean id="myRealm" class="realm.MyRealm" />
</beans>

 其中MyRealm,因为我们这里仅仅是做登录,没有涉及到权限验证,所以直接继承自AuthenticatingRealm即可。代码如下:

 

public class MyRealm extends AuthenticatingRealm{

	private Map<String,String> db = new HashMap<String,String>();
	
	{
		db.put("张三","aaa");
		db.put("李四", "bbb");
	}	
	
	@Override    
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		final String username = (String) token.getPrincipal();
		final String password = db.get(username);
		if(db.get(username)==null){
			throw new UnknownAccountException();
		}
		AuthenticationInfo info = new AuthenticationInfo() {
			private static final long serialVersionUID = -1174132738824136785L;
			public PrincipalCollection getPrincipals() {
				SimplePrincipalCollection coll = new SimplePrincipalCollection(username,"DB");//暂时没有考虑中间修改用户名的情况
				Object o  = coll.getPrimaryPrincipal();//表示是从数据库中来的,这里的DB仅仅是象征意义,
				System.out.println(o.getClass().getName());
				return coll;
			}
			public Object getCredentials() {//暂时没有考虑中间修改密码的情况。
				return password;
			}
		};
		return info;
	}
}

 

 

这个是访问的controller
@Controller
public class ShiroControllerLogin {

	@ResponseBody
	@RequestMapping("/login.do")
	public String denglu(String username,String password,HttpSession session){
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		Subject user = SecurityUtils.getSubject();
		try {
			user.login(token);	
		} catch (IncorrectCredentialsException e1) {
			return "密码不正确";
		}catch(UnknownAccountException e2){
			return "用户名不存在";
		}
		/**
		 * 你的登陆操作假设已经完成
		 */
		return "成功登陆";
	}	
}

 

 希望你也能做到跟我完全一样的代码,然后在下一节中我将尽可能多的查看shiro关于登录的源码。

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    shiro-core-1.6.0.jar

    该资源是目前最新的shiro-core核心包,暂时魏安全性较高的版本,没有发现漏洞版本。提供登录安全性的校验,该资源来自官网,仅供学习参考,请前往官网下载

    shiro-jwt-oauth权限认证

    通过这两个模块,开发者可以学习如何在实际项目中集成和配置Shiro、JWT以及OAuth2.0,以实现安全且灵活的权限认证。这不仅有助于提高系统的安全性,还能提升用户体验,因为用户只需要登录一次即可访问多个受保护的...

    JFinal-Shiro-JDBC-Demo-master.zip_DEMO_jfinal_shiro

    4. **过滤器配置**:Shiro通过Filter链来实现安全控制,如登录拦截、权限校验等。在JFinal中,我们通常会看到对应的ShiroFilter配置,用于定义哪些URL需要经过Shiro的处理。 5. **数据库集成**:JDBC的使用意味着...

    shiro-root-1.4.0-RC2.rar

    1. **Basic**:基础示例,展示了如何初始化Shiro、创建`Subject`、登录和登出的基本流程。 2. **Web**:针对Web应用的示例,包含过滤器配置,演示了如何在Servlet容器(如Tomcat)中集成Shiro以保护Web资源。 3. *...

    springboot+shiro+mybatis-plus.7z

    在本案例中,Shiro主要用来实现用户权限的管理,如登录验证、角色分配和权限校验。你可以通过Shiro的过滤器链来定义哪些URL需要用户登录才能访问,哪些操作需要特定的角色权限。 **MyBatis-Plus** MyBatis-Plus是在...

    shiro视频教程-最全,通俗易懂

    通过上述知识点的学习,Java开发者及相关专业和技术爱好者将能够深入理解Shiro框架的核心概念与工作原理,并能够运用到实际项目开发中,实现用户认证、授权、加密等功能,从而提升项目的整体安全性和用户体验。

    shiro登录拦截校验demo

    "shiro登录拦截校验demo"是一个实际应用Shiro框架进行登录验证和权限拦截的示例项目。 在该demo中,我们可以学习到以下几个核心知识点: 1. **Shiro的基本概念**: - **身份验证(Authentication)**:确认用户...

    shiro学习示例

    在"shiro学习示例"中,我们可以深入理解Shiro的核心概念和实际应用。 1. **Shiro基本概念** - **认证**:确认用户身份的过程,即用户输入用户名和密码,系统通过验证来确认用户的身份。 - **授权**:确定用户是否...

    SSM整合Shiro-登录案例.zip

    通过这个案例,开发者可以学习到如何在实际项目中结合SSM和Shiro,实现一个完整的用户登录功能。这不仅涉及到了前后端交互、数据库操作,还涵盖了权限管理和会话控制等核心安全概念。理解并掌握这些知识,对于提升...

    shiro-Demo01

    1. **配置Shiro**:首先,在项目的配置文件(如:`shiro.ini`)中定义用户、角色和权限。这通常是通过ini-style配置文件完成的,但也可以自定义 Realm 实现数据库驱动的认证和授权。 2. **创建Realm**: Realm是...

    shiro-web.zip

    1. **Filter配置**:Shiro通过Web Filter实现安全控制,我们需要在`web.xml`中配置Shiro的过滤器,如`FormAuthenticationFilter`用于表单登录,`AuthorizationFilter`用于权限校验。 2. ** Realm配置**:在Spring ...

    shiro-springboot.zip

    Apache Shiro 是一个强大且易用的 Java 安全框架,提供身份认证、授权、加密和会话管理功能,简化了企业级应用...通过学习和实践,开发者能够更好地理解 Shiro 的工作原理,并将其应用于实际项目中,提高系统的安全性。

    SpringMVC-Activiti5.16-Shiro-EasyUI项目整合

    开发者可以通过Shiro的API来定制登录、注销、权限校验等逻辑,实现灵活的安全策略。 EasyUI则是一个基于jQuery的前端框架,提供了丰富的UI组件,如表格、树形控件、下拉框等,大大简化了前端页面的开发。在本项目中...

    2.2 SpringBoot与Shiro整合-权限管理实战-课堂笔记.docx

    1. **用户认证**:使用Shiro的Subject接口处理用户登录,校验用户凭证(如用户名和密码)。 2. **用户授权**:定义角色和权限,通过Role-Based Access Control (RBAC)来控制用户对资源的访问。 3. **拦截器**:配置...

    shiro 学习资料

    Shiro 的身份认证过程包括认证发起、凭证匹配、权限校验等步骤。用户提交用户名和密码等凭证后,Shiro 会与事先存储的凭证进行比对,如果匹配成功,就认为用户已通过身份验证。Shiro 支持多种凭证类型,如简单的...

    spring boot shiro demo项目

    1. **Shiro的基本概念** - **认证**:验证用户身份的过程,即验证用户名、密码等信息是否正确。 - **授权**:确定用户是否有操作特定资源的权限,也就是常说的权限校验。 - **加密**:用于保护敏感数据,如密码...

    shiro学习笔记.rar

    可以根据需求自定义过滤器,例如实现登录拦截、权限校验等。在`shiro.ini`配置文件或Java配置中定义过滤器链,指定过滤器的执行顺序和处理逻辑。 **7. 安全会话管理** Shiro提供了会话管理功能,可以控制会话超时、...

    最详细Shiro学习资料(源码)

    Shiro(Apache Shiro)是一个强大且易于使用的Java安全框架,用于身份验证、授权、加密和会话管理等安全功能。它提供了简单的API和灵活的配置选项,可以方便地集成到现有的Java应用程序中。 Shiro的主要特点包括: ...

    Shiro权限管理 的jar包 ,一些example ,shiro资料笔记与核心单词

    - **shiro资料笔记与核心单词**:用户的学习笔记,可能涵盖了关键概念和常用API,是学习Shiro的好帮手。 在实际应用中,Shiro可以通过简单的配置实现用户的登录、权限检查、会话管理等功能。通过 Realm,我们可以...

    Shiro1.2.2_源码(压缩包)

    1. **身份认证(Authentication)**:Shiro 提供了认证流程的核心组件,如 Subject、Realms 和 Authenticator。Subject 是 Shiro 的核心接口,代表当前用户或系统中的“安全实体”。 Realm 是连接应用数据源的桥梁,...

Global site tag (gtag.js) - Google Analytics