java attributes用法_Java AttributePrincipal.getAttributes方法代码示例

本文介绍了如何使用AuthorizationFilter进行身份验证,通过请求头信息从LDAP获取属性,与MOTU服务配置进行匹配,决定是否授权访问。核心涉及到了LDAP认证、MOTU参数解析及服务动作判断。

import org.jasig.cas.client.authentication.AttributePrincipal; //导入方法依赖的package包/类

/**

* Authorization Filter

*

* @param servletResponse object that contains the response the servlet sends to the client

* @param servletRequest object that contains the request the client has made of the servlet.

* @param filterChain object to describe filter chain

*

* @throws IOException the IO exception

* @throws ServletException the servlet exception

*/

@Override

public final void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain)

throws IOException, ServletException {

// Get Request,Response,Session

final HttpServletRequest request = (HttpServletRequest) servletRequest;

final HttpServletResponse response = (HttpServletResponse) servletResponse;

// Get LDAP attributes from request

Principal p = request.getUserPrincipal();

AttributePrincipal principal = (AttributePrincipal) p;

Map attributes = principal.getAttributes();

// boolean isDefaultActionListServicei = false;

// try {

// Load MOTU configuration

MotuConfig conf = BLLManager.getInstance().getConfigManager().getMotuConfig();

// Get the MOTU service parameter (Get default service just in case)

String service = CommonHTTPParameters.getServiceFromRequest(request);

String defService = conf.getDefaultService();

// Get the MOTU action parameter (and the default action)

String action = CommonHTTPParameters.getActionFromRequest(request);

// isDefaultActionListServicei = conf.getDefaultActionIsListServices();

// Authorization (only for service)

boolean authorized = false;

if (service != null) {

authorized = match_ldap_vs_motu(attributes, conf, service); // (1) given service check

} else {

if (action != null) {

if (action.equals(MotuRequestParametersConstant.ACTION_LIST_SERVICES) || action.equals(MotuRequestParametersConstant.ACTION_PING)

|| action.equals(MotuMonitoringParametersConstant.ACTION_DEBUG)

|| action.equals(MotuRequestParametersConstant.ACTION_REFRESH)) {

authorized = true; // (2) public services

} else {

// if (isDefaultActionListServicei)

// authorized = true;

// else

authorized = match_ldap_vs_motu(attributes, conf, defService); // (3) default service

// check

}

} else {

// if (isDefaultActionListServicei)

// authorized = true;

// else

authorized = match_ldap_vs_motu(attributes, conf, defService); // (3) default service

// check

}

}

// Not authorized (unauthorized/forbidden html page)

if (!authorized) {

response.sendError(HttpServletResponse.SC_FORBIDDEN);

return;

}

// Authorized = Everything ok

filterChain.doFilter(request, response);

}

以下是对代码每行含义的解释: ```python class dotdict(dict): ``` - 这行代码定义了一个名为 `dotdict` 的类,它继承自 Python 的内置 `dict` 类。这意味着 `dotdict` 类将拥有 `dict` 类的所有属性和方法,并且可以在此基础上进行扩展。 ```python """dot.notation access to dictionary attributes""" ``` - 这是一个文档字符串(docstring),用于描述这个类的功能。它表明这个类的主要用途是允许使用点号(`.`)来访问字典的属性,而不仅仅是使用传统的方括号(`[]`)语法。 ```python __getattr__ = dict.get ``` - `__getattr__` 是 Python 中的一个特殊方法,当尝试访问对象中不存在的属性时,会调用这个方法。这里将 `__getattr__` 方法设置为 `dict.get`,意味着当使用点号访问 `dotdict` 对象中不存在的属性时,会尝试使用 `dict.get` 方法来获取对应键的值。例如,如果有一个 `dotdict` 对象 `d`,可以使用 `d.key` 来访问 `d` 中键为 `key` 的值,就像使用 `d.get('key')` 一样。 ```python __setattr__ = dict.__setitem__ ``` - `__setattr__` 是另一个特殊方法,当尝试为对象的属性赋值时会调用这个方法。这里将 `__setattr__` 方法设置为 `dict.__setitem__`,意味着当使用点号为 `dotdict` 对象的属性赋值时,会使用 `dict.__setitem__` 方法来设置对应键的值。例如,`d.key = value` 会将 `d` 中键为 `key` 的值设置为 `value`,就像使用 `d['key'] = value` 一样。 ```python __delattr__ = dict.__delitem__ ``` - `__delattr__` 是用于删除对象属性的特殊方法。这里将 `__delattr__` 方法设置为 `dict.__delitem__`,意味着当使用 `del` 语句和点号来删除 `dotdict` 对象的属性时,会使用 `dict.__delitem__` 方法来删除对应键的值。例如,`del d.key` 会删除 `d` 中键为 `key` 的项,就像使用 `del d['key']` 一样。 ### 示例代码 ```python d = dotdict({'name': 'John', 'age': 30}) print(d.name) # 使用点号访问属性 d.city = 'New York' # 使用点号设置属性 del d.age # 使用点号删除属性 print(d) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值