标签: security-context

在父上下文和子上下文中声明Spring Bean

我有一个spring bean(dao)对象,我通过以下xml在我的ServletContext中实例化:

<bean id="userDao" class="com.company.dao.impl.UserDaoImpl">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
Run Code Online (Sandbox Code Playgroud)

这个bean在我的webapp-servlet.xml文件中声明,并由我的应用程序在ServletContext中使用.

我也在使用SpringSecurity.我的理解是,它在不同的上下文中运行(SecurityContext).

我的应用程序有一个webapp-security.xml,我在其中实例化一个自定义身份验证提供程序.我想使用我的应用程序中使用的我的dao也在我的安全上下文中进行用户查找,但是当我运行时:

<bean id="userAuthenticationProvider" class="com.company.security.UserAuthenticationProvider">
    <property name="userDao" ref="userDao" />
</bean>
Run Code Online (Sandbox Code Playgroud)

我得到错误,说没有这样的bean"userDao".bean在我的其他上下文中声明的bean中自动装配好,但不在我的安全上下文中.根据Spring Docs,我相信web.xml中需要两个单独的上下文

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<listener>
    <listener-class>
        org.springframework.security.web.session.HttpSessionEventPublisher
    </listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,如何才能访问我的SecurityContext中存在于我的ServletContext中的DAO?我的dao是否有范围修饰符,或者我可以在运行时在我的身份验证提供程序中以某种方式获取ServletContext?作为参考,这是我想在我的身份验证提供程序中使用它的方式:

public class UserAuthenticationProvider extends
    AbstractUserDetailsAuthenticationProvider {

    @Override
protected UserDetails retrieveUser(String userName,
        UsernamePasswordAuthenticationToken authenticationToken)
        throws AuthenticationException {

    // use dao here
Run Code Online (Sandbox Code Playgroud)

谢谢你向我解释这个

更新:

继续我的调查,似乎我使用我的daos的DispatcherServlet是一个子上下文,安全上下文在某个更高的位置.因此,父上下文无法看到我的DispatcherServlet中的bean.我认为答案是以某种方式将我的bean声明移动到父应用程序上下文中,但我不知道如何做到这一点.这是我的web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/spring-*.xml
    </param-value>
</context-param>

<listener>
    <listener-class>
        org.springframework.security.web.session.HttpSessionEventPublisher
    </listener-class>
</listener>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy
    </filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<servlet> …
Run Code Online (Sandbox Code Playgroud)

security spring servlets ioc-container security-context

24
推荐指数
1
解决办法
3万
查看次数

如何在不同的安全上下文中启动线程?

如何在不同用户的安全上下文中启动线程?当进程正常启动一个线程时,安全上下文也会被传递但是如何在不同安全上下文中使用不同用户的主体启动一个线程?

c# multithreading security-context principal

14
推荐指数
1
解决办法
1万
查看次数

如何自定义SecurityContextPersistenceFilter的行为?

我开发了一个使用基于令牌的身份验证的无状态REST API,我通过SecurityContextHolder.getContext().setAuthentication(authentication)在自定义安全过滤器中调用,手动将Authentication对象添加到安全上下文.我一直遇到上下文没有正确设置的问题,我认为是由于这个原因:

在请求之间存储SecurityContext

在单个会话中接收并发请求的应用程序中,将在线程之间共享相同的SecurityContext实例.即使正在使用ThreadLocal,它也是从HttpSession为每个线程检索的相同实例.如果您希望临时更改运行线程的上下文,则会产生影响.如果您只使用SecurityContextHolder.getContext(),并对返回的上下文对象调用setAuthentication(anAuthentication),则Authentication对象将在共享同一SecurityContext实例的所有并发线程中更改....

您可以自定义SecurityContextPersistenceFilter的行为,为每个请求创建一个全新的SecurityContext,防止一个线程中的更改影响另一个线程.

所以问题是 - 你如何改变SecurityContextPersistenceFilter的行为?

我希望安全上下文不与http会话相关联,但不希望将会话创建策略设置为无状态,因为我仍然希望实现CSRF保护等.

multithreading spring-security security-context servlet-filters spring-boot

12
推荐指数
1
解决办法
599
查看次数

Kubernetes,安全上下文,fsGroup字段和运行容器的默认用户的组ID

我是Kubernetes的新手,正在尝试了解一些安全性知识。

我的问题是关于运行容器的用户的组ID(= gid)。

我使用以下官方示例创建了一个Pod:https : //kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-pod

apiVersion: v1
kind: Pod
metadata:
  name: security-context-demo
spec:
  securityContext:
    runAsUser: 1000
    fsGroup: 2000
  volumes:
  - name: sec-ctx-vol
    emptyDir: {}
  containers:
  - name: sec-ctx-demo
    image: gcr.io/google-samples/node-hello:1.0
    volumeMounts:
    - name: sec-ctx-vol
      mountPath: /data/demo
    securityContext:
      allowPrivilegeEscalation: false
Run Code Online (Sandbox Code Playgroud)

他们在文档中说:

在配置文件中,runAsUser字段指定对于Pod中的任何容器,第一个进程以用户ID 1000运行。所述 fsGroup字段指定组ID 2000与所有相关 的波德容器。组ID 2000还与/ data / demo上安装的卷以及在该卷中创建的任何文件关联。

因此,我进入了容器:

kubectl exec -it security-context-demo -- sh
Run Code Online (Sandbox Code Playgroud)

我看到第一个进程(即使用PID 1的进程)正在以用户1000 => OK运行,这就是我所期望的行为。

 $ ps -f -p 1
 UID        PID  PPID  C STIME TTY …
Run Code Online (Sandbox Code Playgroud)

file-permissions userid security-context user-permissions kubernetes

8
推荐指数
1
解决办法
9636
查看次数

在 pod 中运行 Nginx,无需提权

我试图让 Nginx 以最小的权限运行,同时能够充当端口 80 上的代理。换句话说,这是我正在使用的 securityContext:

            securityContext:
              allowPrivilegeEscalation: false
              readOnlyRootFilesystem: true
              runAsNonRoot: true
              runAsUser: 12321
              runAsGroup: 12321
              privileged: false
              capabilities:
                drop:
                - all
                add: ["NET_BIND_SERVICE"]
Run Code Online (Sandbox Code Playgroud)

据我所知,围绕这个主题设置的大多数 stackoverflow 问题都设置allowPrivilegeEscalationtrue. 除此之外,我发现Bridcrew 的博客将其设置为false,但我无法在我这边重现他们的示例。

我也尝试使用nginxinc/nginx-unprivileged基础图像,但也没有成功。


这是我的 Dockerfile。它由很多chownchmod组成,但我还安装了 libcap2,这样我就可以设置NET_BIND_SERVICE功能并为自定义入口点移动一些文件。

FROM nginx

ENV NGINX_USER="proxy-user" \
    NGINXR_UID="12321" \
    NGINX_GROUP="proxy-group" \
    NGINX_GID="12321"  

RUN set -ex; \
  groupadd -r --gid "$NGINX_GID" "$NGINX_GROUP"; \
  useradd -r --uid "$NGINXR_UID" --gid "$NGINX_GID" "$NGINX_USER" 


# Create empty …
Run Code Online (Sandbox Code Playgroud)

nginx security-context docker kubernetes

8
推荐指数
0
解决办法
855
查看次数

在vb.net中以不同用户身份运行新进程

我目前正在使用一种自行开发的方法在Vista中以不同的用户身份运行一个进程,我无法摆脱那种黑客攻击并且不太理想的感觉(除了它摧毁了UAC,崩溃了我的事实)有安全例外的应用程序,并强制我完全禁用UAC).我的进程包含两个项目(所以两个EXE文件) - 一个"接口"和一个"启动存根" - 这里是过程:

  1. 用户有一个启动"Interface.exe notepad.exe"的快捷方式
  2. Interface.exe有一个表单,要求他们使用他们想要的凭据
  3. Interace.exe使用ProcessStartInfo以新用户身份创建LaunchStub.exe(LS)的实例
  4. LS使用ProcessStartInfo(ShellExecute设置为true)来启动所请求的文件,并且因为它已经作为请求的用户运行,所以新进程也是如此.

我有一个两步过程的原因是我希望用户能够右键单击操作系统具有(.EXE,.SQL,.MSC等)默认操作的任何文件并启动它,并且仅限ProcessStartInfo支持启用"UseShellExecute",但该开关阻止我使用新凭据,所以我一次只能做一个.

这会导致一些问题 - 首先,用户必须已经存在于计算机上,这意味着他们必须先在本地登录.如果该用户没有本地配置文件,则所请求的应用程序有时会启动,但我得到注册表和配置文件例外,因为应用程序期望存在尚未存在的事物(如注册表中的HKCU配置单元,用户不会因为他们从未登录过.

我知道我应该能够将我的应用程序的权限"提升"给他们请求的用户,启动我的新进程,然后撤消提升,但我无法找到一个好的代码示例,并且我不确定它是否允许以完全不同的用户身份运行.这一切都有意义吗?我不禁觉得有更好的方法来做到这一点.


更新:我刚尝试了一些我在网上发现的模拟代码,但无济于事.当与ProcessStartInfo一起使用时,它似乎仍然使用我当前的登录启动进程,而不是我提供的进程,即使我已使用提供的凭据激活模拟.

vb.net impersonation runas startprocessinfo security-context

7
推荐指数
1
解决办法
3万
查看次数

使用Spring Security时,请求之间是否共享SecurityContext?

在使用Spring Boot编写的rest API上使用基于无状态令牌的身份验证时,我看到一些奇怪的行为。

客户端在每个请求中都包含一个JWT令牌,我编写的自定义过滤器扩展了GenericFilterBean,它使用以下内容基于令牌中的声明向安全上下文添加了Authentication对象:

SecurityContextHolder.getContext().setAuthentication(authentication);
Run Code Online (Sandbox Code Playgroud)

并通过执行以下操作来清除上下文:

SecurityContextHolder.getContext().setAuthentication(null);
Run Code Online (Sandbox Code Playgroud)

但是,当我开发的简单应用执行一系列操作时,有时会看到安全上下文设置不正确-有时对于提供令牌的请求而言,它为null。正确地调用了过滤器,还调用了setAuthencation(),但是请求未通过身份验证,并抛出403被拒绝。

如果我通过将会话创建策略设置为STATELESS来明确关闭任何http会话管理,则此行为将停止。

有什么想法会在这里发生吗?安全上下文是否以某种方式在处理请求的线程之间共享?

authentication spring-security thread-safety security-context spring-boot

7
推荐指数
1
解决办法
4950
查看次数

使用 SecurityContext readOnlyRootFilesystem 时的 Kubernetes 137 退出代码

我正在尝试在具有只读文件系统的容器中托管一个网络应用程序。每当我尝试通过容器将根文件系统配置为只读时,SecurityContext我都会收到以下错误:

    Ports:          80/TCP, 443/TCP
    Host Ports:     0/TCP, 0/TCP
    State:          Terminated
      Reason:       Error
      Exit Code:    137
      Started:      Thu, 23 Sep 2021 18:13:08 +0300
      Finished:     Thu, 23 Sep 2021 18:13:08 +0300
    Ready:          False
Run Code Online (Sandbox Code Playgroud)

我尝试使用 AppArmor 配置文件来实现相同的目标,如下所示:

profile parser-profile flags=(attach_disconnected) {
  #include <abstractions/base>
   ...
  deny /** wl,
   ...
Run Code Online (Sandbox Code Playgroud)

不幸的是结果是一样的。

我认为发生的情况是容器无法保存 Web 应用程序的文件并且失败。

在我的场景中,我将运行不受信任的代码,并且必须确保不允许用户访问文件系统。

关于我做错了什么以及如何实现只读文件系统有什么想法吗?

我正在使用 AKS,下面是我的部署配置:

apiVersion: v1
kind: Service
metadata:
  name: parser-service
spec:
  selector:
    app: parser
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    name: http
  - port: 443
    targetPort: …
Run Code Online (Sandbox Code Playgroud)

readonly security-context kubernetes azure-aks

7
推荐指数
1
解决办法
2587
查看次数

如何使用Spring安全性和spring安全对象的属性限制对URL的访问?

我正在使用Spring 5.1和Spring security 4.2.我使用XML文件配置了访问规则.我的问题是,如何根据Spring安全上下文中的属性编写拦截规则(对URL的访问控制)?也就是说,我有一个变量

productList
Run Code Online (Sandbox Code Playgroud)

在安全上下文中,类型为java.util.ArrayList.如果此列表为空或null,我想限制对URL的访问.我怎么写这个?我有

<http name="defaultSecurity" security-context-repository-ref="myContextRepository"
    auto-config="false" use-expressions="true" authentication-manager-ref="authenticationManager"
    entry-point-ref="loginUrlAuthenticationEntryPoint">
    ...
    <intercept-url pattern="/myurl" access="length(principal.productList) > 0" />
    ...
</http>
Run Code Online (Sandbox Code Playgroud)

但当然,上面

length(principal.productList) > 0   
Run Code Online (Sandbox Code Playgroud)

表达是完全错误的.有没有正确的方法来写它?

spring access-control spring-security security-context principal

6
推荐指数
1
解决办法
652
查看次数

具有默认系统身份验证/用户的SecurityContext

在我的春季应用程序中,我希望它SecurityContext始终拥有一个Authentication.如果它不是常规的UsernamePasswordAuthenticationToken,它将PreAuthenticatedAuthenticationToken描述"系统用户".这具有需要用户的不同系统功能的原因.如果没有用户上下文,为了避免特殊处理,我只想添加系统上下文.恕我直言,这也与单一责任原则有关.

为了实现这一点,我可以简单地实现我自己的SecurityContextHolderStrategy并将其设置为SecurityContextHolderwithSecurityContextHolder.setStrategyName(MyStrategyClassName);

现在来问题:

默认SecurityContextHolderStrategyThreadLocalSecurityContextHolderStrategy.我很满意这个策略及其运作方式.我唯一要改变的是getContext()方法.

public SecurityContext getContext() {
    SecurityContext ctx = CONTEXT_HOLDER.get();

    if (ctx == null) {
        ctx = createEmptyContext();
        CONTEXT_HOLDER.set(ctx);
    }
    return ctx;
}
Run Code Online (Sandbox Code Playgroud)

public SecurityContext getContext() {
    SecurityContext ctx = CONTEXT_HOLDER.get();

    if (ctx == null) {
        ctx = createEmptyContext();
        Authentication authentication = new PreAuthenticatedAuthenticationToken("system", null);
        authentication.setAuthenticated(true);
        ctx.setAuthentication(authentication);
        CONTEXT_HOLDER.set(ctx);
    }
    return ctx;
}
Run Code Online (Sandbox Code Playgroud)

这是不是可能的,因为ThreadLocalSecurityContextHolderStrategy类是没有 …

java spring security-context

5
推荐指数
1
解决办法
2545
查看次数