1. 程式人生 > >shiro框架學習筆記(3)---使用shiro完成登陸認證

shiro框架學習筆記(3)---使用shiro完成登陸認證

  在shiro框架中,未登入就是未認證,登陸了才算認證,認證後只能使用一些基本許可權,使用更多的許可權需要授權,認證和授權是不同的概念。

一、UserAction中的相關程式碼

public String login(){
		
		//從Session中獲取生成的驗證碼
		String validatecode = (String) ServletActionContext.getRequest().getSession().getAttribute("key");
		//校驗驗證碼是否輸入正確
		if(StringUtils.isNotBlank(checkcode) && checkcode.equals(validatecode)){
			System.out.println(user.getPassword()+user.getUsername());
			//輸入的驗證碼正確
			//使用shiro框架提供的方式進行認證授權
			Subject subject = SecurityUtils.getSubject();//獲得當前使用者,狀態為未認證
			AuthenticationToken token = new UsernamePasswordToken(user.getUsername(),user.getPassword());//建立使用者名稱密碼令牌物件
			try{
				subject.login(token);//呼叫login
			}catch(Exception e){
				this.addActionError("輸入的賬號或密碼錯誤!");
				e.printStackTrace();
				return LOGIN;
			}
			//沒有異常,驗證正確,將查詢到的user存入session
			//通過subject物件獲得繫結線上程上的user
			User getUser = (User) subject.getPrincipal();
			ServletActionContext.getRequest().getSession().setAttribute("loginUser", getUser);
			return "home";
		}else{
			//輸入的驗證碼錯誤,設定提示資訊,跳轉到登入頁面
			this.addActionError("輸入的驗證碼錯誤!");
			return LOGIN;
		}
	}

未認證時會呼叫這段登陸的程式碼,需要在這段程式碼中加入shiro框架,通過SecurityUtils呼叫當前使用者,並且註冊一個認證令牌

AuthenticationToken ,將使用者輸入的使用者密碼儲存在令牌中,接著呼叫當前使用者的login方法,傳入令牌,接著程式通過applicationContext.xml中配置的realm

二、資料橋樑realm的程式碼:

public class BOSRealm extends AuthorizingRealm{
	@Autowired
	private UserDao userDao;
	
	/**
	 *認證方法
	 */
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		System.out.println("認證方法已經執行");
		//根據使用者名稱查詢資料庫中的密碼,框架負責比對資料庫中的密碼和頁面輸入的密碼是否一致
		UsernamePasswordToken tk = (UsernamePasswordToken) token;
		//獲得使用者名稱
		String username = tk.getUsername();
		//根據使用者名稱查詢資料庫
		User user = userDao.findUserByUsername(username);
		if(user == null){
			//使用者名稱不存在
			return null;
		}
		//簡單認證資訊物件
		AuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), this.getName());
		//框架負責比對資料庫中的密碼和頁面輸入的密碼是否一致
		return info;
	}
	
	/**
	 * 授權方法
	 */
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection args){
		
		return null;
	}

}

如果賬號不存在或者密碼錯誤,都會報相應的錯誤,通過struts.xml定義全域性異常捕捉就可以返回相應的頁面或文字

相關推薦

shiro框架學習筆記(3)---使用shiro完成登陸認證

  在shiro框架中,未登入就是未認證,登陸了才算認證,認證後只能使用一些基本許可權,使用更多的許可權需要授權,認證和授權是不同的概念。 一、UserAction中的相關程式碼 public String login(){ //從Session中獲取生成的

shiro框架學習筆記

最近學習了 許可權框架shiro的知識,做一下 學習的筆記 使用ini 這是shiro 最簡單的用法,首先建立一個demo.ini檔案,裡面寫入如下的內容 [users] xiezihao=123456,admin [roles] admin = user.inse

struts2框架學習筆記3:獲取servletAPI

ucc ati ica 實現 以及 vax color style http Struts2存在一個對象ActionContext(本質是Map),可以獲得原生的request,response,ServletContext 還可以獲得四大域對象(Map),以及param參

shiro安全框架學習筆記

   現在才發現原來安全其實是比較有趣的一個話題。  由於之前已經練過了spring-security的相關內容。所以這裡的shiro只是作為一個知識補充,用作對比學習理解。   由於這個框架相較而言內容比較多一點,所以希望學習的相對詳細一點。&nb

Shiro學習筆記(1)——身份驗證

wan param import println cal 類型 classname zhang ets Shiro——學習筆記(1) 1.核心概念 1.Shiro不會自己去維護用戶、維護權限;這些需要我們自己去設計/提供;然後通過相應的接口註入給Shiro。2.應用代碼直接

Shiro學習筆記(三)

   JavaSE環境下實現使用者登入認證 1、程式碼示例: package com.shiro.exercies; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.Authent

Shiro學習筆記(六)

Shiro整合SpringMVC在Web環境下實現登入認證 Web環境下實現認證的基本流程: 1、jsp頁面:包含使用者資訊,並封裝到form表單中; 2、Spring MVC控制器:處理使用者請求:    - 獲取使用者的登入資訊        - shiro AP

調度框架學習筆記3)—— 集群調度框架的架構演進過程

用戶服 工程師 github 應用層 ext 相對 實習 poll 多維 本章是 The evolution of cluster scheduler architectures 文章的學習筆記。這篇文章討論了這些年調度架構是如何發展的以及為什麽會這樣發展。 首先介紹一下這

排程框架學習筆記3)—— 叢集排程框架的架構演進過程

本章是 The evolution of cluster scheduler architectures 文章的學習筆記。這篇文章討論了這些年排程架構是如何發展的以及為什麼會這樣發展。 首先介紹一下這篇文章的作者:Malte Schwarzkopf,他目前在 MIT 的 PDOS實驗室 作博士後,說起作者的

Mybatis框架學習筆記(2)---使用動態代理完成簡單的增刪改查

使用mybatis的dao層的開發方式有兩種,一種是類似與ssh框架,定義介面,然後定義實現類,還有一種方式就是動態代理方式,即只需要定義介面和方法就可以動態地去執行sql語句。   1、在com.i

一、shiro初始學習,使用ini完成認證

1、建立專案,pom.xml中<dependencies></dependencies>中引入包 <dependency> <groupId>junit</groupId> <artifactId

【Apache Shiro學習筆記——Authentication基礎

從Authentication一步步學習。  先從程式碼開始,執行後再慢慢研究。 以下是我新增的dependecies: <!-- shiro --> <dependency> <groupId>org.apache.shir

shiroshiro學習筆記1-shiro初識

認證流程 環境 程式碼 總結 認證流程 Created with Raphaël 2.1.0Start構造SecurityManager環境Subject.login

ssm+shiro框架搭建筆記(6)

配置shiro (1). 使用maven匯入依賴包。 <!-- shiro start --> <dependency> <groupId>org.apache.shiro</grou

Apache Shiro Subject學習筆記

if ( !currentUser.isAuthenticated() ) { //collect user principals and credentials in a gui specific manner //such as username/password html form, X

shiro學習筆記 過濾器 shiro 表單 驗證碼 登入

自己自定義實現了一個驗證碼錶單過濾器,基於FormAuthenticationFilter 程式碼如下: package cn.ddsxy.ddlx.shiro; import cn.ddsxy.ddlx.util.CaptchaUtil; import org.apac

Spring框架學習筆記3)——SpringMVC框架

SpringMVC框架是基於Spring框架,可以讓我們更為方便的進行Web的開發,實現前後端分離 思路和原理 我們之前仿照SpringMVC定義了一個自定義MVC框架,兩者的思路其實都是一樣的。 建議結合兩篇文章進行學習 JSP學習筆記(6)—— 自定義MVC框架 首先,提供一個前置攔截器(Dispatch

php laravel框架學習筆記 (二) 數據庫操作

true 數據 mar sql show top 一行 ati del 原博客鏈接:http://www.cnblogs.com/bitch1319453/p/6810492.html mysql基本配置 你可用通過配置環境變量,使用cmd進入mysql,當然還有一種東

設計模式:學習筆記(3)——命令者式

抽象 ring code 耦合度 引入 聲明 筆記 操作日誌 string Java設計模式之命令者式 引入命令模式 案列   比如我們要設計一個DOS命令模擬器,它可以接受輸入的命令並做出響應。   0.首先我們的DOS模擬器支持三個大類的功能:文件操作類(FileKit

Redis學習筆記3-Redis5個可運行程序命令的使用

運行程序 檢查 mil 數據文件 img usr pre text mod 在redis安裝文章中,說到安裝好redis後,在/usr/local/bin下有5個關於redis的可運行程序。以下關於這5個可運行程序命令的具體說明。 redis-server Redi