Chinaunix首页 | 论坛 | 博客
  • 博客访问: 225691
  • 博文数量: 36
  • 博客积分: 1188
  • 博客等级: 军士长
  • 技术积分: 802
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-08 21:45
文章分类

全部博文(36)

文章存档

2020年(1)

2017年(2)

2015年(1)

2014年(1)

2013年(1)

2012年(3)

2011年(27)

分类: WINDOWS

2015-11-22 03:48:55

下载源码:LoginExample.zip

  1. use Mojolicious::Lite;

  2. helper auth => sub {
  3.   my ($c, $username, $password) = @_;
  4.   state $users = {tianyv => '123',};
  5.   return unless $users->{$username} eq $password;
  6.   $c->session( user => $username );
  7. };

  8. any '/' => sub {
  9.   my $c = shift;
  10.   my ($username, $password) = map {$c->param($_)} qw/username password/;
  11.   $c->auth($username, $password) if $username;
  12.   $c->render('index');
  13. };

  14. any '/logout' => sub {
  15.   my $c = shift;
  16.   $c->session( expires => 1 );
  17.   $c->redirect_to('/');
  18. };

  19. app->start;

__DATA__


    1. @@ index.html.ep

    2. % my $username = session 'user';
    3. % title $username ? "Welcome back \u$username" : "Not logged in";
    4. % layout 'basic';

    5. <h1><%= title %></h1>

    6. % unless ($username) {
    7.   %= form_for '/' => (class => "form-signin", id => "form-signin") => begin
    8.     %= label_for username => 'Username' => (class => "sr-only")
    9.     %= text_field 'username' => (class => "form-control", placeholder => "Usename", required => "")
    10.     %= label_for password => 'Password' => (class => "sr-only")
    11.     %= password_field 'password' => (class => "form-control", placeholder => "Password", required => "")
    12.     %= submit_button 'Login' => (class => "btn btn-lg btn-primary btn-block")
    13.   % end
    14. % }

    15.   
    16.     <div>
    17.       <p>1、配置</p>
    18.       <pre class="prettyprint">
    19. &lt;!--####影响表单布局####--&gt;
    20. &lt;link href="/css/style.css" rel="stylesheet"&gt;</pre>
    21.           
    22.       <p>2、TT CODE</p>
    23.         <pre class="prettyprint">
    24.  &#37; unless ($username) {
    25.   &#37;= form_for '/' => (class => "form-signin", id => "form-signin") => begin
    26.     &#37;= label_for username => 'Username' => (class => "sr-only")
    27.     &#37;= text_field 'username' => (class => "form-control", placeholder => "Usename", required => "")
    28.     &#37;= label_for password => 'Password' => (class => "sr-only")
    29.     &#37;= password_field 'password' => (class => "form-control", placeholder => "Password", required => "")
    30.     &#37;= submit_button 'Login' => (class => "btn btn-lg btn-primary btn-block")
    31.   &#37; end
    32. &#37; }

    33.  #HTML Code
    34.   &lt;form id="form_body" action="/" class="form-signin" id="form-signin" method="post"&gt;
    35.   &lt;label for="username"&gt;Username class="sr-only"&lt;/label&gt;
    36.   &lt;input name="username" type="text" class="form-control" placeholder="Usename" required=""&gt;
    37.   &lt;label for="password"&gt;Password class="sr-only"&lt;/label&gt;
    38.   &lt;input name="password" type="text" class="form-control" placeholder="Password" required=""&gt;
    39.   &lt;button type="submit" class="btn btn-lg btn-primary btn-block"&gt;Login</button&gt;
    40. &lt;/form&gt;</pre>
    41.     </div>
    42.     
    43.     <div class="row">
    44.       <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 ">
    45.         <p>3、Mojolicious CODE</p>
    46.         
    47.         <pre class="prettyprint">
    48. use Mojolicious::Lite;

    49. helper auth => sub {
    50.   my ($c, $username, $password) = @_;
    51.   state $users = {tianyv => '123',};
    52.   return unless $users->{$username} eq $password;
    53.   $c->session( user => $username );
    54. };

    55. any '/' => sub {
    56.   my $c = shift;
    57.   my ($username, $password) = map {
    58.      $c->param($_)} qw/username password/;
    59.   $c->auth($username, $password) if $username;
    60.   $c->render('index');
    61. };

    62. any '/logout' => sub {
    63.   my $c = shift;
    64.   $c->session( expires => 1 );
    65.   $c->redirect_to('/');
    66. };

    67. app->start;</pre>
    68.    
    69.       </div>
    70.       
    71.       <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 ">
    72.       <p>4、Mojolicious CODE(Plugin 实现)</p>
    73.       
    74.         <pre class="prettyprint">
    75. use Mojolicious::Lite;
    76. plugin 'MyAuth';
    77. #C:\Dwimperl\perl\site\lib\Mojolicious\Plugin\MyAuth

    78. any '/' => sub {
    79.     my $c = shift;
    80.     my $username = $c->param('username');
    81.     $c->auth( 'tianyv', '123' ) if $username;;
    82.     $c->session( user => $username );
    83.     $c->render('index');
    84. };

    85. any '/logout' => sub {
    86.     my $c = shift;
    87.     $c->session( expires => 1 );
    88.     $c->redirect_to('/');
    89. };

    90. app->start;


    91. ###Mojolicious\Plugin\MyAuth###
    92. package Mojolicious::Plugin::MyAuth;
    93. use Mojo::Base 'Mojolicious::Plugin';

    94. sub register {
    95.     my ( $self, $app ) = @_;
    96.     $app->helper(
    97.         auth => sub {
    98.             my ( $self, $username, $password ) = @_;

    99.             return 1
    100.               if $self->param('username') eq $username
    101.                 and $self->param('password') eq $password;
    102.         }
    103.     );
    104. }
    105. 1;</pre>
    106.         
    107.       </div>
    108.     </div>
    109.  


    110. @@ layouts/basic.html.ep
    111. <!DOCTYPE html>
    112. <html>
    113.   <head>
    114.   <meta charset="utf-8">
    115.   <title><%= title %></title>
    116.   <link href="/bootstrap/css/bootstrap.css" rel="stylesheet">
    117.   <link href="/bootstrap/css/prettify.css" rel="stylesheet" >
    118.     
    119.   <link href="/css/style.css" rel="stylesheet">
    120.   
    121.   
    122.         
    123.   <meta name="viewport" content="width=device-wdith,initial-scale=1.0">
    124.   
    125.   </head>
    126.   
    127.   
    128.   <body onload="prettyPrint()">
    129.   <div class="container">
    130.   <header class="header">
    131.           <nav>
    132.             <ul class="nav nav-pills pull-right">
    133.               <li><%= link_to 'Home' => '/' %></li>
    134.               <li><%= link_to 'Logout' => 'logout' %></li>
    135.           </nav>
    136.           <h3 class="text-muted">Mojo-Boostrap3</h3>
    137.       </header>
    138.       
    139.       <div class="container">
    140.         <%= content %>
    141.       </div>
    142.             
    143.     </div>
    144.       <script src="/bootstrap/js/jquery.js"></script>
    145.   </body>
    146.   </br>
    147.   </br>
    148.   </br>
    149.   
    150.   <footer class="footer navbar-fixed-bottom ">
    151.      <span class="footer_bottom"><p class="text-center"> Author:赖韬 | laitaot@126.com &copy;2015 <a href="http://blog.chinaunix.net/uid/22674875.html">Blog:http://blog.chinaunix.net/uid/22674875.html</a></p>
    152.        <p class="text-center">Welcome to the Mojolicious (use Bootstrap3) real-time web framework! </p>
    153.      </span>
    154.   </footer>
    155.   
    156.   </body>
    157.   </div>
    158.   
    159.   <script src="/bootstrap/js/jquery.js"></script>
    160.   <script src="/bootstrap/js/bootstrap.js"></script>
    161.   <script src="/bootstrap/js/prettify.js"></script>
    162. </html>


阅读(2535) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~