Tag: spring security

使用Spring Security的IPfilter

我想知道如何使用Spring Security过滤用户对我的网络应用的访问权限。 我应该扩展AbstractAuthenticationProcessingFilter或类似的东西,并以我自己的方式覆盖它的方法? 如果是这样,您能举例说明web.xml的这种扩展和filter描述示例吗? 提前致谢。 PS在我的应用程序中我也有Spring Security支持(使用默认的org.springframework.web.filter.DelegatingFilterProxy ),但我希望它不仅可以检查用户凭据,还可以检查它们的IP。

Spring Boot CORSfilter – CORS预检频道没有成功

我需要在我的Spring Boot Web应用程序中添加CORSfilter。 我已经添加了CORS映射,如以下文档中所述http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cors.html 这是我的配置: @Configuration @EnableWebMvc public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void addCorsMappings(CorsRegistry registry) { // @formatter:off registry .addMapping(“/**”) .allowedOrigins(CrossOrigin.DEFAULT_ORIGINS) .allowedHeaders(CrossOrigin.DEFAULT_ALLOWED_HEADERS) .allowedMethods(“GET”, “POST”, “PUT”, “DELETE”, “OPTIONS”) .maxAge(3600L); // @formatter:on } … } 现在,当我尝试访问我的API时,我收到以下错误: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://example.com/api/v1.0/user. (Reason: CORS preflight channel […]

使用RestTemplate进行Spring安全身份validation

我有2个春季网络应用程序,提供2组独立的服务。 Web App 1使用基于用户的身份validation实现Spring Security。 现在,Web App 2需要访问Web App 1的服务。通常,我们将使用RestTemplate类向其他Web服务发出请求。 我们如何将Web App 2请求中的身份validation凭据传递给Web App 1

处理Spring Security中基本身份validation的未经授权的错误消息

我试图在我的Web应用程序中使用Spring Security 3.0.5。 基本上,我想要一个通过HTTP GET以json格式返回数据的Web服务。 我已经实现了一个RESTful服务,它在请求url http://localhost:8080/webapp/json时返回数据。 使用以下curl命令可以正常工作 > curl http://localhost:8080/webapp/json {“key”:”values”} 使用spring security添加基本身份validation后,我可以使用以下命令获取数据 > curl http://localhost:8080/webapp/json Apache Tomcat/6.0.29 – Error report ….. > curl -u username:password http://localhost:8080/webapp/json {“key”:”values”} 前一个命令返回标准的tomcat错误页面,因为它现在需要用户名和密码。 我的问题是,是否有可能以打印出我自己的错误消息的方式处理拒绝访问? 即 > curl http://localhost:8080/webapp/json {“error”:”401″, “message”:”Username and password required”} 这是我的spring安全配置和AccessDeniedHandler 。 正如您所看到的,我正在尝试添加access-denied-handler ,它只是通过servlet响应打印出一个字符串,但它仍然不会在命令行上打印我自己的消息。 … AccessDeniedHandler.java package webapp.error; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import […]

如何将基于方法的安全性添加到Spring Boot项目?

我想为Spring Boot项目添加基于方法的安全性。 似乎我只需要添加PermissionEvaluator和MethodSecurityExpressionHandler bean,使用@EnableGlobalMethodSecurity(prePostEnabled = true)注释我的WebSecurityConfigurerAdapter ,使用@PreAuthorize(“isAuthenticated() and hasPermission(#param, ‘somePermissionName’)”) 。 但是在添加PermissionEvaluator bean之后 @Bean public PermissionEvaluator permissionEvaluator() { HelloPermissionEvaluator bean = new HelloPermissionEvaluator(); return bean; } 我得到一个IllegalArgumentException :“需要一个ServletContext来配置默认的servlet处理”: Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘defaultServletHandlerMapping’ defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method […]

Spring Security在运行时注销用户

我正在实现一个基于Spring的Web应用程序,它使用Spring Security和DaoAuthenticationProvider。 因此我创建了一个具有布尔值isEnabled()的用户类; 方法因为它实现了Springs UserDetails接口。 因此,如果用户“未启用”,则此用户将无法再登录。 到现在为止还挺好。 如果我在运行时仍然登录时禁用用户,(似乎)此用户保持登录状态,直到http会话结束,但我希望用户在设置禁用后立即登出。 我怎样才能做到这一点? 谢谢。

Spring Boot + Oauth2客户端凭据

我正在尝试使用带有客户端凭据流的Oath2来保护我在Spring Boot上的微服务。 顺便说一句,那些微服务只会通过中间件层互相交谈,我的意思是不需要用户凭证来允许授权(用户登录过程如Facebook)。 我在Internet上查找了示例,了解如何创建授权和资源服务器来管理此通信。 但是我刚刚找到了解释如何使用用户凭据(三条腿)来解释它的示例。 有没有人有任何示例如何在Spring Boot和Oauth2中做到这一点? 如果可以提供有关所使用范围的更多详细信息,则令牌交换将不胜感激。

使用AuthenticationFailureHandler在Spring Security中自定义身份validation失败响应

目前,每当用户validation失败时,Spring安全性响应: {“error”: “invalid_grant”,”error_description”: “Bad credentials”} 我想通过以下响应代码来增强此响应: {“responsecode”: “XYZ”,”error”: “invalid_grant”,”error_description”: “Bad credentials”} 经过一番探讨,看起来我需要做的就是实现一个AuthenticationFailureHandler,我已经开始做了。 但是,每当我提交无效的登录凭据时,似乎都无法访问onAuthenticationFailure方法。 我已经逐步完成了代码,并在onAuthenticationFailure方法中进行了登录,以确认它未被访问。 我的失败处理程序是: @Component public class SSOAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler{ @Override public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { super.onAuthenticationFailure(request, response, exception); response.addHeader(“responsecode”, “XYZ”); } } 我的WebSecurityConfigurerAdapter包含: @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired SSOAuthenticationFailureHandler authenticationFailureHandler; @Override protected […]

在Spring Boot 1.4中测试安全性

我正在尝试使用SecurityConfig类中定义的自定义安全设置来测试@WebMvcTest : @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers(“/admin*”).access(“hasRole(‘ADMIN’)”).antMatchers(“/**”).permitAll().and().formLogin(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().withUser(“user”).password(“password”).roles(“ADMIN”); } } 测试类是: @RunWith(SpringRunner.class) @WebMvcTest(value = ExampleController.class) public class ExampleControllerMockMVCTest { @Autowired private MockMvc mockMvc; @Test public void indexTest() throws Exception { mockMvc.perform(get(“/”)) .andExpect(status().isOk()) .andExpect(view().name(“index”)); } […]

Spring Security HTTP基本身份validation

我正在尝试使用Spring Security进行非常简单的基本身份validation。 我已正确配置命名空间,服务器中没有例外。 在我的“servlet.xml”中,我获得了Spring Security的下一个: 它几乎都是完美的:非POST的方法不会提示任何登录表单, POST方法会提示它。 问题是,既不是cucu ,也不是bob可以登录那里。 任何人都可以看到我做错了什么? 提前致谢! 😉