Zend Framework2-用户登录创建(并在Module验证) using AuthenticationService with rememberMe

商BC1590年世界历史地图矢量文件SHP格式.zip 本资源为《商BC1590年世界历史地图矢量文件SHP格式》压缩包,文件大小约 17.96 MB,便于集中下载、整理和使用。具体内容请结合压缩包内的实际文件与说明查看。 在当前可读取的目录中,可见属性表(.dbf)、投影说明文件(.prj)、Shapefile 主文件(.shp)、Shapefile 索引(.shx)。建议解压后保持原有目录结构,先浏览文件清单与说明,再选择需要的资料进行加载或整理。 应用方面,可根据实际资料用于学习练习、资料检索、研究整理或项目参考。使用时可先确定所需内容,建立清晰的文件分类,再按项目要求进行筛选、转换与汇总。 使用 Shapefile 数据时,请把实际配套的 .shp、.shx、.dbf 等文件保存在同一目录,并保持同名关联;加载后再检查坐标系与中文字段显示。如需叠加其它数据,请先统一坐标与字段口径。 数据来源、时间范围、坐标系、字段定义、精度和使用条件,请以实际文件及原始说明为准。本介绍不对文件名之外尚未核实的信息作额外承诺。 立即下载


1. Prepare a Login Form with this entity.

//module/SanAuth/src/SanAuth/Model/User.php
namespace SanAuth\Model;

use Zend\Form\Annotation;

/**
 * @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty")
 * @Annotation\Name("User")
 */
class User
{
    /**
     * @Annotation\Type("Zend\Form\Element\Text")
     * @Annotation\Required({"required":"true" })
     * @Annotation\Filter({"name":"StripTags"})
     * @Annotation\Options({"label":"Username:"})
     */
    public $username;
    
    /**
     * @Annotation\Type("Zend\Form\Element\Password")
     * @Annotation\Required({"required":"true" })
     * @Annotation\Filter({"name":"StripTags"})
     * @Annotation\Options({"label":"Password:"})
     */
    public $password;
    
    /**
     * @Annotation\Type("Zend\Form\Element\Checkbox")
     * @Annotation\Options({"label":"Remember Me ?:"})
     */
    public $rememberme;
    
    /**
     * @Annotation\Type("Zend\Form\Element\Submit")
     * @Annotation\Attributes({"value":"Submit"})
     */
    public $submit;
}

Create custom Auth Storage that extends Zend\Authentication\Storage.

//module/SanAuth/src/SanAuth/Model/MyAuthStorage.php
namespace SanAuth\Model;

use Zend\Authentication\Storage;

class MyAuthStorage extends Storage\Session
{
    public function setRememberMe($rememberMe = 0, $time = 1209600)
    {
         if ($rememberMe == 1) {
             $this->session->getManager()->rememberMe($time);
         }
    }
    
    public function forgetMe()
    {
        $this->session->getManager()->forgetMe();
    } 
}

2. Register that into ServiceManager in Module class.

//module/SanAuth/Module.php
namespace SanAuth;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\Authentication\Storage;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;

class Module implements AutoloaderProviderInterface
{
    public function getAutoloaderConfig(){/*common code*/}
    public function getConfig(){ /*common code*/}
    
    public function getServiceConfig()
    {
        return array(
            'factories'=>array(
		'SanAuth\Model\MyAuthStorage' => function($sm){
		    return new \SanAuth\Model\MyAuthStorage('zf_tutorial');  
		},
		
		'AuthService' => function($sm) {
                    //My assumption, you've alredy set dbAdapter
                    //and has users table with columns : user_name and pass_word
                    //that password hashed with md5
		    $dbAdapter           = $sm->get('Zend\Db\Adapter\Adapter');
                    $dbTableAuthAdapter  = new DbTableAuthAdapter($dbAdapter, 
                                              'users','user_name','pass_word', 'MD5(?)');
		    
		    $authService = new AuthenticationService();
		    $authService->setAdapter($dbTableAuthAdapter);
                    $authService->setStorage($sm->get('SanAuth\Model\MyAuthStorage'));
		     
		    return $authService;
		},
            ),
        );
    }

}

3. Create the Auth Controller

//module/SanAuth/src/SanAuth/Controller/AuthController.php
namespace SanAuth\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Form\Annotation\AnnotationBuilder;
use Zend\View\Model\ViewModel;

use SanAuth\Model\User;

class AuthController extends AbstractActionController
{
    protected $form;
    protected $storage;
    protected $authservice;
    
    public function getAuthService()
    {
        if (! $this->authservice) {
            $this->authservice = $this->getServiceLocator()
                                      ->get('AuthService');
        }
        
        return $this->authservice;
    }
    
    public function getSessionStorage()
    {
        if (! $this->storage) {
            $this->storage = $this->getServiceLocator()
                                  ->get('SanAuth\Model\MyAuthStorage');
        }
        
        return $this->storage;
    }
    
    public function getForm()
    {
        if (! $this->form) {
            $user       = new User();
            $builder    = new AnnotationBuilder();
            $this->form = $builder->createForm($user);
        }
        
        return $this->form;
    }
    
    public function loginAction()
    {
        //if already login, redirect to success page 
        if ($this->getAuthService()->hasIdentity()){
            return $this->redirect()->toRoute('success');
        }
                
        $form       = $this->getForm();
        
        return array(
            'form'      => $form,
            'messages'  => $this->flashmessenger()->getMessages()
        );
    }
    
    public function authenticateAction()
    {
        $form       = $this->getForm();
        $redirect = 'login';
        
        $request = $this->getRequest();
        if ($request->isPost()){
            $form->setData($request->getPost());
            if ($form->isValid()){
                //check authentication...
                $this->getAuthService()->getAdapter()
                                       ->setIdentity($request->getPost('username'))
                                       ->setCredential($request->getPost('password'));
                                       
                $result = $this->getAuthService()->authenticate();
                foreach($result->getMessages() as $message)
                {
                    //save message temporary into flashmessenger
                    $this->flashmessenger()->addMessage($message);
                }
                
                if ($result->isValid()) {
                    $redirect = 'success';
                    //check if it has rememberMe :
                    if ($request->getPost('rememberme') == 1 ) {
                        $this->getSessionStorage()
                             ->setRememberMe(1);
                        //set storage again 
                        $this->getAuthService()->setStorage($this->getSessionStorage());
                    }
                    $this->getAuthService()->getStorage()->write($request->getPost('username'));
                }
            }
        }
        
        return $this->redirect()->toRoute($redirect);
    }
    
    public function logoutAction()
    {
        $this->getSessionStorage()->forgetMe();
        $this->getAuthService()->clearIdentity();
        
        $this->flashmessenger()->addMessage("You've been logged out");
        return $this->redirect()->toRoute('login');
    }
}

4. Create Success Controller

//module/SanAuth/src/SanAuth/Controller/SuccessController.php
namespace SanAuth\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class SuccessController extends AbstractActionController
{
    public function indexAction()
    {
        if (! $this->getServiceLocator()
                 ->get('AuthService')->hasIdentity()){
            return $this->redirect()->toRoute('login');
        }
        
        return new ViewModel();
    }
}

5. Register controller invokables, router, and view_manager into module.config.php

return array(
    'controllers' => array(
        'invokables' => array(
            'SanAuth\Controller\Auth' => 'SanAuth\Controller\AuthController',
            'SanAuth\Controller\Success' => 'SanAuth\Controller\SuccessController'
        ),
    ),
    'router' => array(
        'routes' => array(
            
            'login' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/auth',
                    'defaults' => array(
                        '__NAMESPACE__' => 'SanAuth\Controller',
                        'controller'    => 'Auth',
                        'action'        => 'login',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'process' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/[:action]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
            ),
            
            'success' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/success',
                    'defaults' => array(
                        '__NAMESPACE__' => 'SanAuth\Controller',
                        'controller'    => 'Success',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/[:action]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
            ),
            
        ),
    ),
    'view_manager' => array(
        'template_path_stack' => array(
            'SanAuth' => __DIR__ . '/../view',
        ),
    ),
);

6. Create a login view :

//module/SanAuth/view/san-auth/auth/login.phtml
$form = $this->form;
$form->setAttribute('action', $this->url(
    'login/process',
    array(
        'action'     => 'authenticate',
    )
));
$form->prepare();

echo $this->form()->openTag($form);
echo $this->formCollection($form);
echo $this->form()->closeTag();

if ($this->messages){
    echo '<ul>';
    foreach($this->messages as $message)
    {
        echo '<li>'.$message.'</li>';
    }
    echo '</ul>';
}

7. The success view

<!-- //module/SanAuth/view/san-auth/success/index.phtml -->
Login Success.
<br />

<a href="<?php echo $this->url('login/process', array('action'=>'logout')); ?>">Logout</a>


--------在Module.php中调用----------

namespace SanCommons;

use Zend\Mvc\MvcEvent,
    Zend\ModuleManager\Feature\AutoloaderProviderInterface,
    Zend\ModuleManager\Feature\ConfigProviderInterface;

class Module implements
    AutoloaderProviderInterface,
    ConfigProviderInterface
{
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager        = $e->getApplication()->getEventManager();
        $eventManager->attach('route', array($this, 'AuthCheck'), 2);
    }

    public function AuthCheck(MvcEvent $e)
    {
        $application   = $e->getApplication();
    $sm            = $application->getServiceManager();
    
        $router        = $sm->get('router');
    $request       = $sm->get('request');
    
    $matchedRoute = $router->match($request);
    if (null !== $matchedRoute) {
            $controller = $matchedRoute->getParam('controller');
            $action = $matchedRoute->getParam('action');
            
            // check auth...
            $response = $e->getResponse();
            if (! $sm->get('AuthService')->hasIdentity()){
                 $url    = $router->assemble(array(), array('name' => 'login'));
                 
                 $response->setStatusCode(302);
                 $response->getHeaders()->addHeaderLine('Location', $url);
            }
        }
    }
    public function getAutoloaderConfig(){ /*common code */}
    public function getConfig(){ /* common code */ }
}


DeepSeek智慧政务数字政府AI大模型微调设计方案.docx 立即下载

相关推荐

大型央企十五五企业级数据编织(Data Fabric)架构与主动元数据管理平台初步设计方案.docx

大型央企十五五企业级数据编织(Data Fabric)架构与主动元数据管理平台初步设计方案.docx

Zend Framework2 - 在 Module.php中检查管理是否登录,而不是到控制器中检查 (实现方法示例)

在ZF2中应使用事件驱动方法实现,  会影响所有模块 能否只作用于当前模块Module的方法?     (This should be done with event in ZF2 for more details:     click here also this code may help you. http://pastebin.com/FFGVCpki)     pu

xmlife的专栏 1057

圆铝杆:电网升级与高性能合金打开圆铝杆市场增长新空间.docx

圆铝杆:电网升级与高性能合金打开圆铝杆市场增长新空间

对于zendframework2的路由配置的说明

经过一下午的摸索,从基本的zendframework2的基础骨骼下面路由经常出问题,于是搜查了一下资料,得到了一份比较详细的路由配置:<?php return array( 'controllers' => array( 'invokables' => array( 'Shop\Controller\Shop' => 'Shop\Controller\S

一名普通码农的菜地 3471

集团长任务Agent稳定性测试与故障恢复能力建设平台建设方案.docx

集团长任务Agent稳定性测试与故障恢复能力建设平台建设方案.docx

低空经济运营体系构建与实施路径规划方案.docx

低空经济运营体系构建与实施路径规划方案.docx

Zendframework登陆注册实例教程

一.服务器配置 说明:这个实例的开发系统环境是windows 7,所用服务器是Nginx1.0.5,Zendframework框架的版本是1.11.9,PHP5.3.6,所用数据库是MySQL 5.1.50 首先从配置Nginx开始,在这里面需要开启URL的rewrite: 这个是我的nginx.conf文件中配置虚拟主机部分 文件:D:\nginx\nginx-1.0.5\conf\ng...

weixin_30877181的博客 235

城市生命线之长距离输水管线的监测与检测.docx

城市生命线之长距离输水管线的监测与检测.docx

ZendFramework2学习笔记 表单过滤、表单验证

ZF2有很多内建的Filter和Validator组件,可以方便地对表单数据进行处理。

相忘于江湖的专栏 3136

集团IT运维智能体(AIOps Agent)自主故障诊断与自愈平台详细设计方案.docx

集团IT运维智能体(AIOps Agent)自主故障诊断与自愈平台详细设计方案.docx

企业大模型应用商店与内部应用上架审核运营平台详细设计方案.docx

企业大模型应用商店与内部应用上架审核运营平台详细设计方案.docx

Zend framework2 - Zend\Authentication和Zend\Acl 用户身份验证与权限控制-Demo参考OK

controller文件夹里包含index和login两个controller文件从login文件说起,它有三个Action一个是loginAction,一个是showAction,一个是indexAction。indexAction用于将登录表单提交上来的数据进行验证,loginAction用与解析登录表单,showAction用于显示一个主表单。这个表单中有三个提交按钮设为add,hello,

xmlife的专栏 1424

大型央企十五五工业大模型私有化部署与生产知识智能化管理平台建设方案.docx

大型央企十五五工业大模型私有化部署与生产知识智能化管理平台建设方案.docx

四旋翼无人机模糊自适应PID控制,俯仰姿态控制律设计研究(Matlab代码、Simulink仿真实现)

内容概要:本文围绕四旋翼无人机的俯仰姿态控制问题,深入研究了模糊自适应PID控制律的设计与实现方法。通过结合经典PID控制与模糊逻辑的自适应能力,提出了一种能够在复杂动态环境中实时调整控制参数的改进型控制策略。研究内容涵盖了系统动力学建模、控制律设计、模糊规则制定以及Matlab/Simulink环境下的仿真实现,重点解决了传统PID控制器在面对外部扰动和模型不确定性时控制性能下降的问题。文中详细展示了控制系统结构设计、仿真模型搭建过程,并通过对比实验验证了所提方法在响应速度、稳定性和抗干扰能力方面的优越性。; 适合人群:具备自动控制理论基础和Matlab/Simulink仿真能力的高校研究生、科研人员及从事无人机飞控系统开发的工程技术人员。; 使用场景及目标:①应用于四旋翼无人机飞行控制系统设计与优化;②为非线性、强耦合系统的先进控制策略研究提供仿真验证平台;③服务于智能控制算法的教学演示与科研复现。; 阅读建议:建议读者结合Matlab代码与Simulink模型同步运行,深入理解模糊推理与PID参数自整定的交互机制,并尝试调整模糊规则和隶属度函数以观察控制性能变化,从而掌握先进控制策略的设计精髓。

某区一云一网一图全域未来乡村数字化建设与共富运营规划方案.docx

某区一云一网一图全域未来乡村数字化建设与共富运营规划方案.docx

【工业无线通信】5GHz全频段大功率AP基站:支持TDMA抗干扰与AI漫游的工业级无线覆盖解决方案设计

内容概要:本文介绍了一款型号为WFS7000XBE-RY的工业级大功率无线AP基站,工作于5GHz全频段(4920-6100MHz),支持300Mbps无线速率和29dBm(0.8瓦)发射功率,采用TDMA时分多址与私有协议技术,实现10台以上基站同频共存、移动中不丢包、通信延迟≤1ms。设备具备IP54防护等级,支持-45~85°C宽温运行,适用于复杂工业环境,提供2km覆盖距离或外接高增益天线实现100km以上桥接。支持多种工作模式与网络功能,如AI FastRoaming极速漫游(切换延迟≤30ms)、QoS、VLAN、MPLS等,确保稳定可靠的无线连接。; 适合人群:工业物联网、智能物流、自动化控制领域的工程师和技术人员,以及需要高稳定性无线通信解决方案的研发与部署人员。; 使用场景及目标:①用于智慧物流、工业4.0、AMR/AGV/RGV小车、四向穿梭车、堆垛机、无人机叉车等移动设备的无线联网;②满足风电监测、室内外无线桥接、车载船载视频传输等远距离、高抗干扰通信需求;③替代传统滑触线和光纤布线,实现免布线、高灵活性的无线数据传输。; 阅读建议:本文适用于关注工业无线通信性能与可靠性的技术人员,建议结合实际部署场景参考其硬件规格、环境适应性和组网能力进行系统设计与选型。

省级科技管理部门如何遴选高质量的科技中介服务机构来协助开展区域技术转移工作.docx

科易网基于行业知识库+行业大数据+科技资源要素+AI模型,以AI,赋能技术转移、成果转化、技术经理人培训、知识产权管理、区域产业分析与布局、地方科技招商、创新创业大赛等场景,推出科技创新领域的AI工具、场景化服务和数智化解决方案,推动科技创新与产业创新智能化发展。

基于javaEE的图书管理系统

极简的图书管理系统,无任何样式修饰,适合新手练手(图文并释) 1、实现了用户注册、登录,图书的添加、修改、删除和修改操作 2、工具需要:eclipse、mysql、Tomcat 3、做系统之前在eclipse需要配置Tomcat服务器和导入mysql连接数据库的jar包 4、数据库中建一个user表存用户信息,一个book表存图书信息 一、各种页面 index.html文件,系统运行的首页,有登录...

qq_44357792的博客 2万+
上一篇: Yii2-设置和获取、删除Cookies空值分析
下一篇: Zend framwork2-ACL(访问控制列表) 示例
xmlife
博客等级 码龄19年 89粉丝 55原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值