我正在尝试使用Spring Cloud的Zuul,Eureka和我自己的服务来实现微服务架构.我有多个具有UI和服务的服务,每个服务都可以使用x509安全性对用户进行身份验证.现在我正试图将Zuul放在这些服务面前.由于Zuul无法将客户端证书转发到后端,我认为下一个最好的方法是在Zuul的前门验证用户身份,然后使用Spring Session在后端服务中复制其身份验证状态.我已经按照Dave Syer 的教程进行了操作,它几乎可以工作,但不是第一次请求.这是我的基本设置:
如果清除所有会话并重新开始并尝试命中代理,则它会使用zuul服务器的证书将请求发送到后端.然后,后端服务根据该用户证书查找用户,并认为用户是服务器,而不是在Zuul代理中经过身份验证的用户.如果您只是刷新页面,那么您突然成为后端的正确用户(在Zuul代理中进行身份验证的用户).我正在检查的方法是在后端控制器中打印出Principal用户.所以在第一次请求时,我看到了服务器用户,在第二次请求时,我看到了真实用户.如果我在后端禁用x509,在第一次请求时,我得到403,然后刷新,它让我进去.
看起来会话没有足够快地复制到后端,所以当用户在前端进行身份验证时,它在Zuul转发请求时没有进入后端.
有没有办法保证会话在第一个请求(即会话创建)上被复制?或者我错过了一步以确保其正常工作?
以下是一些重要的代码段:
Zuul代理:
@SpringBootApplication
@Controller
@EnableAutoConfiguration
@EnableZuulProxy
@EnableRedisHttpSession
public class ZuulEdgeServer {
public static void main(String[] args) {
new SpringApplicationBuilder(ZuulEdgeServer.class).web(true).run(args);
}
}
Run Code Online (Sandbox Code Playgroud)
Zuul配置:
info:
component: Zuul Server
endpoints:
restart:
enabled: true
shutdown:
enabled: true
health:
sensitive: false
zuul:
routes:
service1: /**
logging:
level:
ROOT: INFO
# org.springframework.web: DEBUG
net.acesinc: DEBUG
security.sessions: ALWAYS
server:
port: 8443
ssl:
key-store: classpath:dev/localhost.jks
key-store-password: thepassword
keyStoreType: JKS
keyAlias: localhost
clientAuth: want …Run Code Online (Sandbox Code Playgroud) 所以,我在我的项目中使用spring session.如何使用它在项目中编写集成测试?我应该为春季会议内部模拟一些东西吗?或者使用嵌入式redis的任何合理方式?
我看到过去有一些@EnableEmbeddedRedis注释,但似乎已被删除:https://github.com/spring-projects/spring-session/issues/248
//编辑
我试过通过MockHttpSession
mockMvc.perform(post("/register").session(mockHttpSession)
Run Code Online (Sandbox Code Playgroud)
但春天尝试并且无论如何都无法连接到redis.
是否可以通过login basic和oauth2在一个应用程序中组合authoryzation和身份验证?
我的项目基于jhipster项目,简单的弹簧安全会话登录,现在我需要为移动应用程序添加oauth2安全性,看起来它是不可能的.
现在我有工作其中一个的情况,oauth2确定如果WebSecurityConfigurerAdapter的订单号比ResourceServerConfiguration更大.这意味着如果首先是oauth安全过滤器.我在stackoverflow中阅读了很多内容并尝试了许多解决方案,例如:
Spring security oauth2和表单登录配置,对我来说这是不行的.
现在我知道这与一些安全过滤器冲突有关,但我不知道如何解决它.
如果有人有类似的问题并且他设法做到了,或者知道如何绕过或做得更好,我会很感激这些信息.在此先感谢您的帮助 :)
@Configuration
@EnableWebSecurity
public class SecurityOauth2Configuration extends WebSecurityConfigurerAdapter {
@Inject
private UserDetailsService userDetailsService;
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder());
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers("/scripts/**/*.{js,html}")
.antMatchers("/bower_components/**")
.antMatchers("/i18n/**")
.antMatchers("/assets/**")
.antMatchers("/swagger-ui/index.html")
.antMatchers("/api/register")
.antMatchers("/api/activate")
.antMatchers("/api/account/reset_password/init")
.antMatchers("/api/account/reset_password/finish")
.antMatchers("/test/**");
}
@Configuration
@EnableAuthorizationServer
public static class …Run Code Online (Sandbox Code Playgroud) security spring spring-security spring-security-oauth2 spring-session
我有一个Spring MVC Web应用程序,我有一些我已经注释过的java类,@Scope("session")目前在tomcat上运行它.使用范围注释的类存储用户信息,在服务器崩溃时我不想丢失这些信息.那么如何@Scope("session")将会话与所有流信息以及存储在bean中的用户数据一起保存到外部数据库(自定义noSQL DB)中,这样当服务器崩溃时,用户将无缝地转移到另一个服务器和所有数据和信息以及bean从外部数据库恢复到新服务器,用户甚至没有注意到任何内容.我不想使用tomcat持久性,我也不想使用Redis.
我正在使用spring-data-redis,spring-session和 Spring Boot 连接到我的 Redis 实例。但是,我不想spring-data-redis连接到 DB 0(默认),而是连接到另一个本地数据库(比如 DB 1)。这是我想要存储会话的地方。spring-data-redis 可以实现吗?
我们有几个使用 Java Spring 编写的 Web 应用程序,我们正在使用spring-data-redis和@EnableRedisHttpSession。我想知道春季会议的内部结构是什么。在创建新会话之前,它会检查 Redis 数据库是否有重复的会话密钥?
我查看了 spring 文档,也进行了谷歌搜索,但无法得到明确的答案。
我正在考虑将用户会话从应用程序级别移动到 Redis 实例。我相信我已经根据文档(http://docs.spring.io/spring-session/docs/current/reference/html5/#httpsession)正确设置了所有内容,但我没有看到我的行为期待并认为我在某处错过了一步。
该应用程序当前使用 HttpSession,因此我只是将以下内容添加到上下文中:
<context:annotation-config/>
<util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/>
<beans:bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>
<beans:bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="HOSTNAME" p:port="6379" />
Run Code Online (Sandbox Code Playgroud)
在 web.xml 中添加了以下内容:
<filter>
<filter-name>springSessionRepositoryFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSessionRepositoryFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Run Code Online (Sandbox Code Playgroud)
应用程序构建、部署和加载页面很好,但是当我查看页面上的 cookie 时,我同时拥有 JSESSIONID 和 SESSION。我知道 JSESSIONID 是 Spring Security 使用的,看起来 SESSION 是 Spring Session 使用的。当我查看 redis 时,看起来 SESSION 是存储的那个。
另一个问题是自定义会话对象(使用 session.setAttribute 添加)没有显示在会话中。会话中唯一显示的是登录后,并且是 SPRING_SECURITY_CONTEXT 对象。当我删除 Spring Session 过滤器时,这些对象被添加到会话中就好了。
这是正常行为,还是我的设置有一些奇怪的冲突?
我正在使用Spring Boot 1.3.3来构建 Web 应用程序。我使用Redis来处理会话。
我会将一些“关键”数据设置到 中HttpSession,我想了解这将如何与 Redis 配合使用。是服务器端存储的信息加上浏览器端的密钥还是所有数据都在用户浏览器的cookie中?
我想查看答案的文档参考或获得权威答案(例如 Pivotal dev)。
使用 gradle 在 Spring 会话示例下构建时:https ://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot-findbyusername.html
我遇到了关于 java.annotation 模块的错误,有人知道如何解决吗?
/spring-session/spring-session-core/src/main/java/org/springframework/session/config/annotation/web/http/SpringHttpSessionConfiguration.java:22: error: package javax.annotation is not visible
import javax.annotation.PostConstruct;
^
(package javax.annotation is declared in module java.xml.ws.annotation, which is not in the module graph)
warning: unknown enum constant When.MAYBE
reason: class file for javax.annotation.meta.When not found
1 error
1 warning
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':spring-session-core:compileJava'.
> Compilation failed; see the compiler error output for details.
Run Code Online (Sandbox Code Playgroud)
我尝试在 build.gradle 中添加以下配置,但问题仍然存在。
tasks.withType(AbstractCompile) { …Run Code Online (Sandbox Code Playgroud) 我们使用Spring Boot + Spring Security设计了多租户WebApp.此应用程序用于管理Azure中的某些资源.用户使用OAuth2.0登录我们的WebApp,并可以通过我们的应用程序访问Azure资源.
现在,我们需要允许多个用户在单个浏览器会话中登录我们的应用程序.因此,基本上用户(用户1)将使用credentials1登录以访问这些凭证所允许的资源.然后,用户将使用credentials2(基本上是另一个用户凭据,可以将其称为user2)登录到同一浏览器页面.同一会话中将有两个活动用户.用户应该能够在这些帐户之间切换.
用户登录我们的应用程序后,我们实例化RestTemplate(使用输入的凭据)来访问Azure资源.
要么我们可以将单个JSession id映射到多个RestTemplate,要么将多个JSession ID(在单个JSession cookie中)映射到单个RestTemplate.我们可以有请求参数来指示要使用的RestTemplate.
我们使用SpringSecurity来获取访问令牌.然后,此访问令牌将在RestTemplate中使用,并用于访问Azure资源.
spring spring-security spring-boot spring-security-oauth2 spring-session
spring-session ×10
spring ×6
spring-boot ×3
java ×2
redis ×2
session ×2
gradle ×1
security ×1
spring-bean ×1
spring-cloud ×1
spring-data ×1