【仅供内部供应商使用,不提供对外解答和培训】

Page tree

【仅供内部供应商使用,不提供对外解答和培训】

Skip to end of metadata
Go to start of metadata

接口作用

默认的http认证,局限性比较大,要求认证服务器比如返回字符串"false"用于表示认证失败,返回其他则表示认证通过,且返回的值为角色。而通过http认证处理接口,可以随意的根据需要判断认证成功和失败的条件,此外还提供一个方法用于修改密码。

接口内容

HttpAuthProcessor
package com.fr.stable.fun;

import com.fr.stable.fun.mark.Immutable;
import com.fr.stable.privilege.AuthenticationBridge;

/**
 * @author richie
 * @date 2015-03-20
 * @since 7.1.1
 */
public interface HttpAuthProcessor extends Immutable {

    String MARK_STRING = "HttpAuthProcessor";

    int CURRENT_LEVEL = 1;

    /**
     * 处理HTTP认证返回信息的接口
     * @param authentication 保存认证信息的对象
     * @param responseText 从HTTP请求返回的字符串
     * @return 认证失败则返回false,否则返回true
     */
    boolean process(AuthenticationBridge authentication, String responseText);

    /**
     * 重设密码
     * @param username 用户名
     * @param old 老密码
     * @param nw 新密码
     * @return 重设成功返回true,否则返回false
     */
    boolean changePassword(String username, String old, String nw);
}

示例代码

默认实现
public class DefaultHttpAuthProcessor extends AbstractHttpAuthProcessor {
    public static DefaultHttpAuthProcessor instance = new DefaultHttpAuthProcessor();

    public static DefaultHttpAuthProcessor getInstance() {
        return instance;
    }

    public boolean process(AuthenticationBridge authentication, String responseText) {
        if ("false".equalsIgnoreCase(responseText)) {
            return false;
        } else {
            // alex:如果返回的不是false,表示认证通过
            authentication.setAuthenticated(true);
            String[] roles = responseText.split(",");
            Authority[] authes = new Authority[roles.length];
            for (int i = 0; i < roles.length; i++) {
                authes[i] = new Authority(roles[i]);
            }
            authentication.setAuthorities(authes);
            return true;
        }
    }
}

注册方式

<extra-core>
    <HttpAuthProcessor class="com.fr.plugin.your.classNameXXX">
</extra-core>
  • No labels