我在一些oauth2实现中看到了授权服务器在发出访问令牌时返回的响应的附加信息.我想知道是否有办法使用spring-security-oauth2来实现这一目标.我希望能够在访问令牌响应中包含一些用户权限,以便我的消费应用程序不需要管理用户权限,但仍然可以将用户设置在他们自己的安全上下文中并应用他们自己的任何spring-security检查.
我想另一种选择是使用JWT令牌并与客户端应用程序共享适当的信息,以便他们可以解析令牌中的用户/权限并将其设置在上下文中.这让我更加不舒服,因为我更愿意控制哪些客户端应用程序可以访问此信息(仅限受信任的应用程序),而AFAIK只有授权服务器和资源服务器应该知道如何解析JWT令牌.
我希望能够处理从必须在页面中访问的源读取的Java流.作为第一种方法,我实现了一个分页迭代器,它只是在当前页面用完项目时请求页面,然后用于StreamSupport.stream(iterator, false)获取迭代器上的流句柄.
当我发现我的页面获取非常昂贵时,我想通过并行流访问页面.在这一点上,我发现由于java直接从迭代器提供的spliterator实现,我的天真方法提供的并行性是不存在的.因为我实际上对我想要遍历的元素了解了很多(我知道在请求第一页后总结果数,并且源支持偏移和限制)我认为应该可以实现我自己的分裂器实现真正的并发(在页面元素和查询页面上完成的工作).
我已经能够很容易地实现"在元素上完成的工作"并发性,但在我最初的实现中,页面的查询只能由最顶层的分裂器完成,因此不会从分工中受益由fork-join实现提供.
如何编写实现这两个目标的分裂器?
作为参考,我将提供我到目前为止所做的事情(我知道它没有适当地划分查询).
public final class PagingSourceSpliterator<T> implements Spliterator<T> {
public static final long DEFAULT_PAGE_SIZE = 100;
private Page<T> result;
private Iterator<T> results;
private boolean needsReset = false;
private final PageProducer<T> generator;
private long offset = 0L;
private long limit = DEFAULT_PAGE_SIZE;
public PagingSourceSpliterator(PageProducer<T> generator) {
this.generator = generator;
}
public PagingSourceSpliterator(long pageSize, PageProducer<T> generator) {
this.generator = generator;
this.limit = pageSize;
}
@Override
public boolean tryAdvance(Consumer<? super T> action) {
if (hasAnotherElement()) {
if …Run Code Online (Sandbox Code Playgroud) 最近我用反射做了很多,并实现了这个小实用工具方法.我很惊讶地发现第一个版本没有编译,但后者确实如此.
不编译:
public static <T> Class<T[]> getArrayClassOfType(Class<T> componentType) {
return Array.newInstance(componentType, 0).getClass();
}
Run Code Online (Sandbox Code Playgroud)
编译并正常工作:
public static <T> Class<T[]> getArrayClassOfType(Class<T> componentType) {
Class c = Array.newInstance(componentType, 0).getClass();
return c;
}
Run Code Online (Sandbox Code Playgroud)
两个问题:它有什么问题?有更好的方法吗?
这是第一个编译错误:
TypeUtils.java:[12,60] incompatible types: java.lang.Class<capture#1 of ? extends java.lang.Object> cannot be converted to java.lang.Class<T[]>
Run Code Online (Sandbox Code Playgroud) 在过去的几天里,我一直在使用spring-security-oauth2来实现spring boot/spring security /和java配置.我已经设法解决了大部分困难,但我对现在出了什么问题感到难过.
我正在成功完成以下步骤:
?code=asdfa&state=asdfasf查询字符串此时,我相信无论使用什么,都AuthorizationCodeResourceDetails应该为访问令牌交换授权代码和客户端应用凭证.这是进程因以下堆栈跟踪而失败的地方.
Caused by: org.springframework.security.oauth2.common.exceptions.InvalidRequestException: Possible CSRF detected - state parameter was present but no state could be found
at org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider.getParametersForTokenRequest(AuthorizationCodeAccessTokenProvider.java:246)
at org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider.obtainAccessToken(AuthorizationCodeAccessTokenProvider.java:198)
at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainNewAccessTokenInternal(AccessTokenProviderChain.java:142)
at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainAccessToken(AccessTokenProviderChain.java:118)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.acquireAccessToken(OAuth2RestTemplate.java:221)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.getAccessToken(OAuth2RestTemplate.java:173)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.createRequest(OAuth2RestTemplate.java:105)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:564)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.doExecute(OAuth2RestTemplate.java:128)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:529)
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:261)
at com.pvr.apps.admin.user.UserServiceImpl.getAllUsers(UserServiceImpl.java:51)
at com.pvr.apps.admin.web.IndexController.serveUserList(IndexController.java:35)
Run Code Online (Sandbox Code Playgroud)
客户端上的东西看起来像(我@EnableOAuth2Client在主配置上也有一个注释).
@Component
public class UserServiceImpl implements UserService {
@Resource
@Qualifier("accessTokenRequest")
private AccessTokenRequest accessTokenRequest;
public OAuth2ProtectedResourceDetails createResource() {
AuthorizationCodeResourceDetails resourceDetails = new AuthorizationCodeResourceDetails(); …Run Code Online (Sandbox Code Playgroud) 我已经开始使用 RQ/Redis 为我的 django 站点构建一些长时间运行的作业的异步执行。我希望做如下事情:
我想要一个模型的每个实例一个队列。你可以把这个模型想象成一个 api 用户帐户。(这些不会很多。最多 15 - 20 个)
我将在队列中均匀地分配批次的任务(从 10 到 500 之间的任意位置)。在第一批完成之前可以添加多批。
对于每个批次,我想为每个没有积极处理的队列启动一个工作程序,我想以批处理模式运行这些工作程序,以便一旦它们用完任务,它们就会关闭。
我意识到我不能以批处理模式运行它们,然后我将始终在所有队列上工作/监听工作。这样做的问题是我希望能够动态添加和删除队列,所以最好每批启动可用队列。
我意识到我在队列之间分配任务似乎很奇怪,但原因是同一队列中的每个任务必须根据我使用的服务进行速率限制/节流(将其视为 API速率限制,但每个队列代表不同的帐户)。但就我的目的而言,任务在哪个帐户上运行没有区别,所以我不妨跨所有帐户并行化。
我面临的问题是,如果我启动一个工人并给它一个已经在处理的队列,我现在有两个工人在该队列上独立运行,因此我预期的节流率会减半。如果没有工作人员在该队列上运行,我如何仅启动工作人员?我可能会为此找到一个 hacky 解决方案,但我更愿意以“正确”的方式处理它,因为我对队列没有太多经验,所以我想我应该问一下。
我已经在实现我自己的工作类,以便我可以动态控制队列,所以我只需要一种方法来添加逻辑,如果该队列已经在处理,则不会给它一个新的工作人员。我的工人的一个简单版本在这里:
# custom_worker.py
import sys
from Api.models import *
from rq import Queue, Connection, Worker
# importing the necessary namespace for the tasks to run
from tasks import *
# dynamically getting the queue names in which I am expecting tasks
queues = [user.name for user in ApiUser.objects.all()]
with Connection():
qs = list(map(Queue, queues)) or [Queue()] …Run Code Online (Sandbox Code Playgroud) 警告:我意识到这可能是对 spring bean 和/或 Java 8 的默认接口方法背后意图的滥用。我正在寻找的是对为什么这可能是一种我未能认识到的不安全方法的具体且合理的批评。
我定义了一个类,它使我可以静态访问正在运行的应用程序上下文:
@Component
public class BeanAccessor implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public static <T> T getSingleton(Class<T> clazz){
return applicationContext.getBean(clazz);
}
public static <T> T getSingleton(String beanName, Class<T> clazz){
return applicationContext.getBean(beanName, clazz);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
BeanAccessor.applicationContext = applicationContext;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我可以在默认接口方法中使用这些 BeanAccessor 方法,以便从方法内部访问 spring 管理的 bean。
我意识到可以通过实现其他类和服务来实现此功能,而不是觉得需要将它混合到当前的类和服务中(至少对于下面的示例,我实际上并没有对“this”做任何事情)。这在一定程度上是一种“艺术”偏好,我相信我会继续寻找其他用例。
请注意,我并没有像这里描述的那样尝试保留每个实例的状态https://kerflyn.wordpress.com/2012/07/09/java-8-now-you-have-mixins/我确实认识到存在危险在这样做。
这是一个示例用法:
public interface ClientAware {
String TENANT_NAME = "TENANT_NAME";
default ClientDetails clientDetails() {
ClientDetailsService service …Run Code Online (Sandbox Code Playgroud) 我刚刚开始学习scala,今天我决定编写一个CSV解析器,它可以很好地加载到case类中,但是将数据存储在Shapeless的HList对象的行(列表)中,这样我就可以接触到类型级编程了.
这是我到目前为止所拥有的:
// LoadsCsv.scala
import shapeless._
import scala.collection.mutable
trait LoadsCsv[A, T <: HList] {
val rows: mutable.MutableList[T] = new mutable.MutableList[T]
def convert(t: T)(implicit gen: Generic.Aux[A, T]): A = gen.from(t)
def get(index: Int): A = {
convert(rows(index))
}
def load(file: String): Unit = {
val lines = io.Source.fromFile(file).getLines()
lines.foreach(line => rows += parse(line.split(",")))
}
def parse(line: Array[String]): T
}
Run Code Online (Sandbox Code Playgroud)
并且正在加载数据集的对象:
// TennisData.scala
import shapeless._
case class TennisData(weather:String, low:Int, high:Int, windy:Boolean, play:Boolean)
object TennisData extends LoadsCsv[TennisData, String :: Int :: Int :: …Run Code Online (Sandbox Code Playgroud) 我想知道是否可以执行以下操作:
var obj = {
counter: (function(){
if(!this.val){ this.val = 0; }
this.val += 1;
return this.val;
})();
};
console.log(obj.counter); //should output 1
console.log(obj.counter); //should output 2
console.log(obj.counter); //should output 3
...
Run Code Online (Sandbox Code Playgroud)
有没有办法从这样的对象中获取字段,以便每次访问该函数时都会重新评估一个函数?
java ×4
java-8 ×2
spring ×2
django ×1
generics ×1
java-stream ×1
javascript ×1
python ×1
python-rq ×1
redis ×1
reflection ×1
scala ×1
shapeless ×1
spliterator ×1