Tag: spring security

使用具有不同AuthenticationProviders的多个WebSecurityConfigurerAdapter(用于Web应用程序的API和LDAP的基本身份validation)

根据Spring Security Reference 5.7节 ,应该可以定义多个安全适配器。 我尝试做同样但没有成功。 在服务器重新启动之后,API的前x次正常使用基本身份validation,但是经过几次我被重定向到登录(表单)页面,这应该只针对我们的Web应用程序,而不是API调用。 我的代码: @EnableWebSecurity public class MultiHttpSecurityConfig { @Configuration @Order(1) public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter { @Autowired private Environment env; @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication(). withUser(“admin”).password(“pw_test”).roles(API_ROLE); } protected void configure(HttpSecurity http) throws Exception { http .antMatcher(“/services/**”) .authorizeRequests() .anyRequest().hasRole(API_ROLE) .and() .httpBasic() .and() .csrf() .disable(); } } […]

如何在没有XML配置的情况下定义自定义AuthenticationEntryPoint

我已经挣扎了好几天了。 我是Spring Boot的新手,并且喜欢不使用XML配置的想法。 我创建了一个RESTfull应用程序(使用JSON)。 我正在按照本教程正确配置身份validation。 我认为我设法使用Java配置重现几乎所有的配置,除了一件事 – AuthenticationEntryPoint 本教程使用http标记中的属性, 并按以下方式定义formLogin : Spring Security手册中的AuthenticationEntryPoint解释说: 可以使用元素上的entry-point-ref属性设置AuthenticationEntryPoint。 没有提到有关如何使用Java配置执行此操作的任何内容。 那么如何在没有XML的情况下“注册”我自己的restAuthenticationEntryPoint ,以防止在使用formLogin时重定向到登录表单? 下面我将提到我尝试过的内容。 谢谢你们。 在我的尝试中,发现你可以使用basicAuth定义它,如下所示: @Configuration @Order(1) public static class RestWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { if (restAuthenticationEntryPoint == null) { restAuthenticationEntryPoint = new RestAuthenticationEntryPoint(); } http .authorizeRequests() .antMatchers(“/**”).hasAnyRole(Sec.ADMIN,Sec.SUPER_USER) … .and() .httpBasic() .authenticationEntryPoint(restAuthenticationEntryPoint) 但我正在使用这样的表单登录(没有httpBasic部分): […]

即使用户正在使用系统,N分钟后的绝对会话到期

我正在使用带有弹簧安全性的grails web应用程序。 即使会话处于活动状态,我也要求在固定的一组时间后强制会话到期。 我想我可以添加filter并检查每个请求的上次登录时间: if (CURRENT_TIME – LAST_LOGIN > ABSOLUTE_SESSIO EXPIRATION) then FORCE LOGOUT 但问题是,在用户发出请求之前,会话仍然在服务器上处于活动状态。 即使用户使用系统,这可能会在N分钟后立即销毁会话吗? 我正在研究tomcat会话管理以及Spring安全性如何处理它,但没有找到任何有用的信息。 有人能指出我的一些例子吗? 有没有人实现类似的东西?

SpringSecurity – 自定义自动身份validation

这是我的情景: Web应用程序为许多应用程序执行一种SSO 登录用户而不是单击链接,应用程序使用用户信息(名称,pwd [无用],角色)发布到适当的应用程序 我正在其中一个应用程序上实现SpringSecurity,以便从它的function中受益(会话中的权限,其类提供的方法等) 所以,我需要开发一个自定义filter – 我想 – 能够从请求中检索用户信息,从数据库检索,通过自定义DetailsUserService ,有关用户的更多信息(电子邮件等等),然后执行身份validation该用户,根据从请求中检索到的角色。 我正在查看预身份validationfilter,但我不确定它是否是正确的选择。 似乎这些对象在主体已经在会话中时被使用,由一些先前的认证机制放置(是不是?)。 我认为,一旦确定了正确的filter,我应该在以下内容中执行: 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); 这是解决我问题的正确方向吗? 你有什么建议可以帮我找到遗失的东西吗? 谢谢你们, 卢卡 加成: 嗨Xearxess! 很抱歉再次打扰你,但似乎根据SpringSecurity 2.0.4的代码翻译比我想象的更难:S问题是XML …我尝试了不同的配置,但我总是遇到命名空间问题,缺少属性等等…… 引用CUSTOM-FILTER元素的2行是两次不同的尝试,它们都被标记为错误。 如何指定filter作为属性的位置? 此外,auth manager定义上的身份validation提供程序引用也标记为错误。 我认为我也需要将它指定为属性,对吧? 希望你能给我最后一次推动;)再次感谢你, 卢卡

Thymeleaf的Spring Security简单示例

嗨,我正在尝试按照一个简单的例子来做一个简单的登录表单页面,我在这个页面中找到http://docs.spring.io/autorepo/docs/spring-security/4.0.x/guides/form.html 问题是我每次尝试登录时都会收到此错误我收到此错误: Expected CSRF token not found. Has your session expired? Expected CSRF token not found. Has your session expired? 当我收到此错误时,我按下浏览器中的后退按钮并尝试第二次登录,当我这样做时,我收到此错误: HTTP 403 – Invalid CSRF Token ‘null’ was found on the request parameter ‘_csrf’ or header ‘X-CSRF-TOKEN’ 在教程页面中是这条消息: We use Thymeleaf to automatically add the CSRF token to our form. If we were not […]

spring安全。 如何注销用户(撤销oauth2令牌)

当我想要注销时,我调用此代码: request.getSession().invalidate(); SecurityContextHolder.getContext().setAuthentication(null); 但在它之后(在使用旧的oauth令牌的下一个请求中)我调用 SecurityContextHolder.getContext().getAuthentication(); 我在那里看到我的老用户 怎么解决?

使用Spring Security 3进行哈希和腌制密码

如何使用Spring Security 3散列密码并加密它们?

在JPA中使用Spring Security

我是Spring的新手。 我们正在使用弹簧安全function。 数据库连接:JPA的eclipselink实现。 数据库:MySql 使用spring security时,身份validation提供程序的配置如下, – 但是在JPA中我们没有定义数据源,我们使用持久性单元和提供者jpa.PersistenceProvider. 那么我们如何配置身份validation提供程序以便使用JPA进行数据库连接? data-source-ref字段究竟应该包含什么才能使用数据库进行身份validation? 先谢谢你。

在java config中添加http安全filter

我试图在春季添加网络安全性,但我不希望filter适用于某些事情。 怎么在java中完成? 也许还有一种更好的方法可以做到这一点,因为我创建了一个自定义filter,但这是我认为实例化它的唯一方法,因为它的依赖性。 总的来说,我想要做的是: /resources/**不应该通过filter, /login (POST)不应该通过filter,其他一切都应该通过filter 通过我在spring发现的各种例子我能够想出一个开始,但它显然不起作用: @Configuration @EnableWebSecurity @Import(MyAppConfig.class) public class MySecurityConfig extends WebSecurityConfigurerAdapter { @Override public void configure(WebSecurity webSecurity) throws Exception { webSecurity.ignoring().antMatchers(“/resources/**”); } @Override public void configure(HttpSecurity httpSecurity) throws Exception { httpSecurity .authorizeRequests() .antMatchers(“/resources/**”).permitAll() .antMatchers(“/login”).permitAll(); httpSecurity.httpBasic(); httpSecurity.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); } @Bean @Autowired public TokenFilterSecurityInterceptor tokenInfoTokenFilterSecurityInterceptor(MyTokenUserInfoCache userInfoCache, ServerStatusService serverStatusService, HttpSecurity httpSecurity) throws Exception { […]

如何使用Spring Security / Spring MVC处理表单登录

简单的问题,我只需要一个指向正确方向的指针: 我有一个简单的Spring MVC / Spring Security webapp。 最初我设置了Spring Security,以便默认登录页面正确显示和validation(我使用DaoAuthenticationProvider实现了UserDetailsService来执行此操作)。 下一步:使用我的登录页面替换默认的spring登录页面并发布凭据。 但是我如何处理提交的登录凭据? 我假设我将表单发布到控制器,validation凭据,但我不清楚在此之后正确的步骤是什么。 例如: 我在调用AuthenticationManager的方法吗? 我需要为此定义一个bean吗? 我需要像AuthenticationEntryPoint那样实现一个接口/服务吗? 我已经完成了3次文档,并没有完全遵循它们。 我知道这很简单,所以我只需要听听过程应该如何流动。