调整OAuth2 结构

This commit is contained in:
zhouhao
2017-04-07 09:28:29 +08:00
parent 86a22c5a87
commit 2f87b3aaad
19 changed files with 491 additions and 66 deletions

View File

@@ -30,4 +30,11 @@
<artifactId>hsweb-authorization-oauth2-client</artifactId>
<dependencies>
<dependency>
<groupId>org.hswebframework.web</groupId>
<artifactId>hsweb-authorization-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2016 http://www.hswebframework.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package org.hswebframework.web.authorization.oauth2.client;
import org.hswebframework.web.authorization.Authentication;
import org.hswebframework.web.authorization.oauth2.client.listener.OAuth2Event;
import org.hswebframework.web.authorization.oauth2.client.listener.OAuth2Listener;
import org.hswebframework.web.authorization.oauth2.client.request.OAuth2Session;
import org.hswebframework.web.authorization.oauth2.client.response.OAuth2Response;
import java.util.List;
import java.util.function.Function;
/**
* @author zhouhao
*/
public interface OAuth2RequestService {
OAuth2SessionBuilder create(String serverId);
void registerListener(String serverId, OAuth2Listener<? extends OAuth2Event> listener);
void doEvent(String serverId, OAuth2Event event);
void doEvent(String serverId, OAuth2Event event, Class<? extends OAuth2Event> eventType);
static void main(String[] args) {
OAuth2RequestService requestService = null;
Function<OAuth2Response, Authentication> authExchanger = null;
String authorizationCode = "";
OAuth2Session session = requestService
.create("hsweb-user-center")
.byAuthorizationCode(authorizationCode);
Authentication authentication = session
.request("oauth2/user-auth-info")
.get()
.as(Authentication.class);
session.request("menu")
.param("paging", "0")
.get().as(List.class);
authentication.getUser().getId();
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2016 http://www.hswebframework.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package org.hswebframework.web.authorization.oauth2.client;
/**
* TODO 完成注释
*
* @author zhouhao
*/
public interface OAuth2ServerConfig {
String name = "name";
String describe = "describe";
String apiBaseUrl = "apiBaseUrl";
String authUrl = "authUrl";
String accessTokenUrl = "accessTokenUrl";
String clientId = "clientId";
String clientSecret = "clientSecret";
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2016 http://www.hswebframework.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package org.hswebframework.web.authorization.oauth2.client;
import org.hswebframework.web.authorization.oauth2.client.request.OAuth2Session;
/**
* @author zhouhao
*/
public interface OAuth2SessionBuilder {
OAuth2Session byAuthorizationCode(String code);
OAuth2Session byClientCredentials();
OAuth2Session byPassword(String username, String password);
OAuth2Session byImplicit();
OAuth2Session refreshToken(String refreshToken);
OAuth2Session byAccessToken(String accessToken);
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2016 http://www.hswebframework.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package org.hswebframework.web.authorization.oauth2.client.listener;
import java.util.Optional;
import java.util.function.Function;
/**
* TODO 完成注释
*
* @author zhouhao
*/
public class OAuth2CodeAuthBeforeEvent implements OAuth2Event {
private String code;
private String state;
private Function<String, Object> parameterGetter;
public OAuth2CodeAuthBeforeEvent(String code, String state, Function<String, Object> parameterGetter) {
this.code = code;
this.state = state;
this.parameterGetter = parameterGetter;
}
public String getCode() {
return code;
}
public String getState() {
return state;
}
@SuppressWarnings("unchecked")
public <T> Optional<T> getParameter(String name) {
return Optional.ofNullable((T) parameterGetter.apply(name));
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2016 http://www.hswebframework.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package org.hswebframework.web.authorization.oauth2.client.listener;
/**
* TODO 完成注释
*
* @author zhouhao
*/
public interface OAuth2Event {
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2016 http://www.hswebframework.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package org.hswebframework.web.authorization.oauth2.client.listener;
/**
* TODO 完成注释
*
* @author zhouhao
*/
public interface OAuth2Listener<T extends OAuth2Event> {
void on(T event);
}

View File

@@ -0,0 +1,138 @@
/*
* Copyright 2016 http://www.hswebframework.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package org.hswebframework.web.authorization.oauth2.client.request;
import org.hswebframework.web.authorization.oauth2.client.response.OAuth2Response;
import java.util.function.Consumer;
/**
* OAuth2请求接口,用于发起OAuth2请求
*
* @author zhouhao
*/
public interface OAuth2Request {
/**
* 设置路径参数,如url为:/user/{userId} .设置pathParam("userId","admin"),将自动解析url为 /user/admin
*
* @param name 参数名称
* @param value 参数值
* @return request自身
*/
OAuth2Request pathParam(String name, Object value);
/**
* 设置请求参数,相当于/url?name=value
*
* @param name 参数名称
* @param value 参数值
* @return request自身
*/
OAuth2Request param(String name, Object value);
/**
* 设置请求体,将内容根据contentType(默认application/json)序列化为对应的请求数据
*
* @param value 请求内容
* @return request自身
*/
OAuth2Request requestBody(Object value);
/**
* 设置请求头
*
* @param name 名称
* @param value 值
* @return request自身
*/
OAuth2Request header(String name, String value);
/**
* 设置cookie
*
* @param name 名称
* @param value 值
* @return request自身
*/
OAuth2Request cookie(String name, String value);
/**
* 设置请求的contentType
*
* @param contentType
* @return request自身
* @see "application/json"
*/
OAuth2Request contentType(String contentType);
/**
* 设置接受响应的格式,相当与请求头:Accept
*
* @param accept
* @return request自身
* @see "application/json"
*/
OAuth2Request accept(String accept);
/**
* 设置请求超时时间,超时后回调 timeoutConsumer
*
* @param millisecond 超时时间(毫秒),小于0则不设置超时
* @param timeoutCallBack 超时后的处理回调
* @return request自身
* @see Consumer
*/
OAuth2Request timeout(long millisecond, Consumer<OAuth2Request> timeoutCallBack);
/**
* 以GET方式请求,并返回请求结果
*
* @return 请求结果
*/
OAuth2Response get();
/**
* 以PUT方式请求,并返回请求结果
*
* @return 请求结果
*/
OAuth2Response put();
/**
* 以POST方式请求,并返回请求结果
*
* @return 请求结果
*/
OAuth2Response post();
/**
* 以DELETE方式请求,并返回请求结果
*
* @return 请求结果
*/
OAuth2Response delete();
/**
* 以PATCH方式请求,并返回请求结果
*
* @return 请求结果
*/
OAuth2Response patch();
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2016 http://www.hswebframework.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package org.hswebframework.web.authorization.oauth2.client.request;
import java.io.Serializable;
/**
* @author zhouhao
*/
public interface OAuth2Session extends Serializable {
OAuth2Session connect();
OAuth2Request request(String uriOrUrl);
OAuth2Session param(String name, Object value);
void close();
boolean isClosed();
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2016 http://www.hswebframework.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package org.hswebframework.web.authorization.oauth2.client.response;
import java.util.List;
import java.util.function.Function;
/**
* @author zhouhao
*/
public interface OAuth2Response {
String asString();
byte[] asBytes();
<T> T as(Function<OAuth2Response, T> exchangeFunction);
<T> T as(Class<T> type);
<T> List<T> asList(Class<T> type);
int status();
}

View File

@@ -1,45 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2016 http://www.hswebframework.org
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
~
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>hsweb-authorization-oauth2-server</artifactId>
<groupId>org.hswebframework.web</groupId>
<version>3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hsweb-authorization-oauth2-server-entity</artifactId>
<dependencies>
<dependency>
<groupId>org.hswebframework.web</groupId>
<artifactId>hsweb-commons-entity</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.hswebframework.web</groupId>
<artifactId>hsweb-authorization-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

View File

@@ -28,15 +28,11 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>hsweb-authorization-oauth2-server</artifactId>
<packaging>pom</packaging>
<modules>
<module>hsweb-authorization-oauth2-server-entity</module>
<module>hsweb-authorization-oauth2-server-dao</module>
<module>hsweb-authorization-oauth2-server-api</module>
<module>hsweb-authorization-oauth2-server-simple</module>
<module>hsweb-authorization-oauth2-server-controller</module>
<module>hsweb-authorization-oauth2-server-model</module>
<module>hsweb-authorization-oauth2-server-starter</module>
</modules>
<dependencies>
<dependency>
<groupId>org.hswebframework.web</groupId>
<artifactId>hsweb-commons-entity</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

View File

@@ -18,8 +18,9 @@
package org.hswebframework.web.authorization.oauth2.api;
import org.hswebframework.web.entity.authorization.oauth2.OAuth2AccessEntity;
import org.hswebframework.web.entity.authorization.oauth2.OAuth2ClientEntity;
import org.hswebframework.web.authorization.oauth2.api.entity.OAuth2AccessEntity;
import org.hswebframework.web.authorization.oauth2.api.entity.OAuth2ClientEntity;
/**
* @author zhouhao

View File

@@ -16,9 +16,8 @@
*
*/
package org.hswebframework.web.entity.authorization.oauth2;
package org.hswebframework.web.authorization.oauth2.api.entity;
import org.hswebframework.web.authorization.User;
import org.hswebframework.web.commons.entity.GenericEntity;
import org.hswebframework.web.commons.entity.RecordCreationEntity;
@@ -49,7 +48,7 @@ public interface OAuth2ClientEntity extends GenericEntity<String>, RecordCreatio
/**
* @return 客户端所有者
* @see User#getId()
* @see org.hswebframework.web.authorization.User#getId()
*/
String getOwnerId();