ofbiz 的登录机制

ofbiz 的登录比较完善,并且实现了单点登录,下面是笔者记录的ofbiz登录的基本过程.
org.ofbiz.securityext.login.LoginEvents
中有静态变量
保存了所有登陆的用户和用户登录的webapp.这样为单点登录提供了很大的方便.

在controller.xml中登录配置:

<request-map uri="login">
      
<security https="true" auth="false"/>
      
<event type="java" path="org.ofbiz.securityext.login.LoginEvents" invoke="login"/>
      
<response name="success" type="view" value="main"/>
      
<response name="error" type="view" value="login"/>
    
</request-map>

在输入用户名和密码后,ofbiz的前端控制器将调用org.ofbiz.securityext.login.LoginEvents类中的静态方法login.

  1. 得到用户名和密码并处理大小写.

 

String username = request.getParameter("USERNAME" );
        String password 
= request.getParameter("PASSWORD"
);

        
if (username == null) username = (String) session.getAttribute("USERNAME"
);
        
if (password == null) password = (String) session.getAttribute("PASSWORD"
);

        
if ((username != null&& ("true".equalsIgnoreCase(UtilProperties.getPropertyValue("security.properties""username.lowercase")))) 
{
            username 
=
 username.toLowerCase();
         }

        
if ((password != null&& ("true".equalsIgnoreCase(UtilProperties.getPropertyValue("security.properties""password.lowercase"))))  {
            password 
=
 password.toLowerCase();
         }

 

  1. 判断是否登录

 

if ("true".equalsIgnoreCase(UtilProperties.getPropertyValue("security.properties""login.lock.active")))  {
            boolean userIdLoggedIn 
= isLoggedInSession(username, request, false
);
            boolean thisUserLoggedIn 
= isLoggedInSession(username, request, true
);
            
if (userIdLoggedIn && !thisUserLoggedIn) 
{
                request.setAttribute(
"_ERROR_MESSAGE_""<b>This user is already logged in.</b><br>"
);
                
return "error"
;
             }

         }

 

准备visit

// get the visit id to pass to the userLogin for history
        String visitId = VisitHandler.getVisitId(session);

 

 visit = delegator.makeValue("Visit"null );
                    Long nextId 
= delegator.getNextSeqId("Visit"
);
visit.
set("visitId"
, nextId.toString());
                        visit.
set("sessionId"
, session.getId());
                        visit.
set("fromDate"new
 Timestamp(session.getCreationTime()));
InetAddress address 
=
 InetAddress.getLocalHost();

                            
if (address != null
{
                                visit.
set("serverIpAddress"
, address.getHostAddress());
                                visit.
set("serverHostName"
, address.getHostName());
                            }
 else  {
                                Debug.logError(
"Unable to get localhost internet address, was null"
, module);
                             }

 visit.create();
                            session.setAttribute(
"visit", visit);


进行验证

result = dispatcher.runSync("userLogin", UtilMisc.toMap("login.username", username, "login.password", password, "visitId", visitId));

处理验证结果(1.判断是否具有基本权限)

ComponentConfig.WebappInfo info =  ComponentConfig.getWebAppInfo(serverId, contextPath);
 String permission 
=
 info.getBasePermission();
                
if (!"NONE".equals(permission) && !security.hasEntityPermission(permission, "_VIEW", userLogin)) 
{
                    
return false
;
                 }


(2.完成基本的登录过程)

session.setAttribute("userLogin" , userLogin);
        
// let the visit know who the user is

        VisitHandler.setUserLogin(session, userLogin, false );
        loginToSession(userLo
gin, request);


  loginToSession(userLogin, request);
表示在静态变量中loggedInSessions加入 userLoginId 和webappName session.getId().

 

public static Map loggedInSessions = new HashMap();

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值