SSH集成Shiro,实现认证

xiaoxiao2021-02-28  97

1、首先写一个简单登录界面 <body> <form action="studentloginstu.action" method="post"> <input type="text" name="student.sname"/><br/> <input type="text" name="student.spwd"/><br/> <input type="submit" value="登录"/> </form> </body> 2、写一个相对应的Action public class StudentAction extends ActionSupport{ private StudentBiz studentBiz; private Student student; public String loginstu() throws Exception { System.out.println("登录........."); //读取Subject对象 Subject subject=SecurityUtils.getSubject(); UsernamePasswordToken token=new UsernamePasswordToken(student.getSname(),student.getSage()+""); System.out.println(student.getSname()+" "+student.getSage()); try { //执行认证 subject.login(token); } catch (Exception e) { //e.printStackTrace();注释不打印错误 return "login";   } return "success"; } public StudentBiz getStudentBiz() { return studentBiz; } public void setStudentBiz(StudentBiz studentBiz) { this.studentBiz = studentBiz; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } } 3、配置自定义realmpublic class MyRealm extends AuthorizingRealm{ private StudentBiz studentBiz; //授权 @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) { return null; } //认证 @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { //获取传递过来的数据 UsernamePasswordToken usernamePasswordToken=(UsernamePasswordToken) token; String uname=usernamePasswordToken.getUsername(); String upass=usernamePasswordToken.getPassword().toString(); System.out.println("认证~:"+uname+" "+upass); //查询数据库 String pwd=studentBiz.getPwd(uname); System.out.println("pwd:"+pwd); //认证,把认证的结果 返回 SimpleAuthenticationInfo simpleAuthenticationInfo=new SimpleAuthenticationInfo(uname, pwd, this.getName()); return simpleAuthenticationInfo; } public StudentBiz getStudentBiz() { return studentBiz; } public void setStudentBiz(StudentBiz studentBiz) { this.studentBiz = studentBiz; } } 4、写一个spring配置文件(applicationContext-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"> <import resource="applicationContext-biz.xml"/> <!-- 1.配置shiroFilter --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <!-- 引入SecurityManager --> <property name="securityManager" ref="securityManager"></property> <!-- 配置登录界面--> <property name="loginUrl" value="/login.jsp"></property> <!-- 成功页面 --> <property name="successUrl" value="/success.jsp"></property> <!-- 过滤 登录才允许 --> <property name="filterChainDefinitions"> <value> /login.jsp*=anon /success.jsp*=authc /studentloginstu.action*=authc </value> </property> </bean> <!-- 2.配置SecurityManager --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="myRealm"></property> </bean> <!-- 3.配置自定义realm --> <bean id="myRealm" class="com.zking.shiro.MyRealm"> <property name="studentBiz" ref="studentBizImp"></property> </bean> </beans> 5、配置web.xml文件<web-app>   <!-- 加载spring配置文件 --> <!-- needed for ContextLoaderListener --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-*.xml</param-value> </context-param> <!-- 加载shiro过滤 --> <filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <!-- 加载struts2 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <!-- Bootstraps the root web application context before servlet initialization --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app> 6、struts配置文件<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"     "http://struts.apache.org/dtds/struts-2.3.dtd">     <struts>     <package name="myPackage" extends="struts-default">     <action name="student*" class="allAction" method="{1}">     <result name="success">/success.jsp</result>     <result name="login">/login.jsp</result>     </action>     </package>     </struts>
转载请注明原文地址: https://www.6miu.com/read-42315.html

最新回复(0)