这是我的情景:
所以,我需要开发一个自定义过滤器 - 我猜 - 能够从请求中检索用户信息,从数据库检索,通过自定义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) 我无法使Log4j2 在日志文件上写入Spring和Spring Security日志消息(后者我绝对需要解决问题)。我在这里和其他地方已经读了很多书(当然,在Spring参考书中),但是我无法找到适合我的情况的解决方案。
重要的是要注意,在以下两种解决方案中,地雷日志消息和休眠消息均已正确写入文件中。该问题仅影响Spring消息。
据我了解,Spring使用jcl(公共日志),并且有两种方法可以让Spring使用Log4j2:
我试图从所有其他依赖项中排除commons-logging,以便手动控制其包含。在第一个解决方案中,我让它包括在内,在第二个解决方案中,我排除了它,但是我对此主题已有不同的见解。无论如何,在两种情况下,Spring都不记录。
我可以看到hibernate使用jboss-logging。这是为什么它在两种配置下都起作用的原因吗?Spring日志记录是否可以包含它?
应用服务器可以在这方面出问题吗?也许默认情况下加载了一些库,这些库将覆盖应用程序库,使事情一团糟?
这是我的环境:
我猜这里有一个依赖问题:一些不应该存在或缺少的库。
编辑:
从我的依赖项树中,我没有看到spring-coreon 的依赖项commons-logging,如引用所述。我可以看到一个spring-data-jpa在slf4j和jcl-over-slf4j。jcl-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)