小编Dol*_*fiz的帖子

SpringSecurity - 自定义自动身份验证

这是我的情景:

  • Web应用程序为许多应用程序执行一种SSO
  • 登录用户而不是单击链接,应用程序发出一个帖子,其中包含用户信息(名称,pwd [无用],角色)
  • 我正在其中一个应用程序上实现SpringSecurity,以便从其功能(会话中的权限,其类提供的方法等)中受益

所以,我需要开发一个自定义过滤器 - 我猜 - 能够从请求中检索用户信息,从数据库检索,通过自定义DetailsUserService,有关用户的更多信息(电子邮件等等),然后执行身份验证该用户,根据从请求中检索到的角色.

我在看预身份验证过滤器,但我不确定它是否是正确的选择.似乎当主体已经在会话中时,预期会使用这些对象,由某些先前的身份验证机制放置(是不是?).

我认为,一旦确定了正确的过滤器,我应该在以下内容中执行:

GrantedAuthority[] ga= new GrantedAuthority[1];
ga[0] = new GrantedAuthorityImpl(myUser.getRole());

SecurityContext sc = SecurityContextHolder.getContext();
Authentication a = new UsernamePasswordAuthenticationToken(userName, userPwd, ga);
a = authenticationManager.authenticate(a);
sc.setAuthentication(a);
Run Code Online (Sandbox Code Playgroud)

这是解决我问题的正确方向吗?你有什么建议可以帮助我找到遗失的东西吗?

谢谢你们,

卢卡

加成:

嗨Xearxess!很抱歉再次打扰你,但似乎根据SpringSecurity 2.0.4翻译你的代码比我想象的更困难:S问题是XML ...我尝试了不同的配置,但我总是遇到命名空间问题,缺少属性等等......

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:security="http://www.springframework.org/schema/security"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
              http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">  

    <security:http>
        <security:intercept-url pattern="/**" access="isAuthenticated()" />
        <security:logout logout-url="/logout" logout-success-url="http://milan-ias-vs.usersad.everis.int/DMTest/" invalidate-session="true" />
        <security:custom-filter position="PRE_AUTH_FILTER" ref="preAuthenticatedProcessingFilter" />
    </security:http>

    <bean id="preAuthenticatedProcessingFilter" class="it.novartis.ram.authentication.PreAuthenticatedProcessingFilter">
        <custom-filter position="PRE_AUTH_FILTER"/>
        <property name="authenticationManager" …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security

8
推荐指数
2
解决办法
2万
查看次数

Log4j2和Spring 4

我无法使Log4j2 在日志文件上写入Spring和Spring Security日志消息(后者我绝对需要解决问题)。我在这里和其他地方已经读了很多书(当然,在Spring参考书中),但是我无法找到适合我的情况的解决方案。

重要的是要注意,在以下两种解决方案中,地雷日志消息和休眠消息均已正确写入文件中。该问题仅影响Spring消息。

据我了解,Spring使用jcl(公共日志),并且有两种方法可以让Spring使用Log4j2:

  • 只需添加网桥log4j-jcl(jcl-> log4j2)
  • 将SLF4J与jcl-over-jcllog4j-slf4j-impl一起使用(jcl-> slf4j-> log4j2)

我试图从所有其他依赖项中排除commons-logging,以便手动控制其包含。在第一个解决方案中,我让它包括在内,在第二个解决方案中,我排除了它,但是我对此主题已有不同的见解。无论如何,在两种情况下,Spring都不记录。

我可以看到hibernate使用jboss-logging。这是为什么它在两种配置下都起作用的原因吗?Spring日志记录是否可以包含它?

应用服务器可以在这方面出问题吗?也许默认情况下加载了一些库,这些库将覆盖应用程序库,使事情一团糟?

这是我的环境:

  • Java 1.6
  • Servlet 2.5
  • Oracle Weblogic 12.1
  • Spring MVC 4.3.4。发布
  • Spring Security 4.2.0。发布
  • Log4j 2.3(最新版本与Java 6兼容)
  • Hibernate实体管理器4.2.21.final
  • SLF4J 1.7.22

我猜这里有一个依赖问题:一些不应该存在或缺少的库。

编辑: 从我的依赖项树中,我没有看到spring-coreon 的依赖项commons-logging,如引用所述。我可以看到一个spring-data-jpaslf4jjcl-over-slf4jjcl-over-sl4j正如rgoers建议的那样,shl 的存在应该是问题所在。我将更新结果...

因此,这是我的pom.xml依赖项部分(如您所见,有两个不同的日志记录依赖项配置,其中一个已注释;此配置都使我的和休眠日志运行良好,但是两个都不使Spring日志运行):

<properties>
    <spring.version>4.3.4.RELEASE</spring.version>
    <spring.security.version>4.2.0.RELEASE</spring.security.version>
    <!--<spring.data.jpa.version>1.10.5.RELEASE</spring.data.jpa.version>-->
    <spring.data.jpa.version>1.7.2.RELEASE</spring.data.jpa.version>
    <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
    <log4j.version>2.3</log4j.version>
    <hibernate.validator.version>5.3.2.Final</hibernate.validator.version>
    <hibernate.entitymanager.version>4.2.21.Final</hibernate.entitymanager.version> <!-- LAST VERSION SUPPORTING JPA …
Run Code Online (Sandbox Code Playgroud)

spring slf4j log4j2

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

标签 统计

spring ×2

java ×1

log4j2 ×1

slf4j ×1

spring-security ×1