Tag: spring security

@AuthenticationPrincipal与Spring Boot无法正常工作

使用Spring Boot 1.3.1,我遇到了@AuthenticationPrincipal问题。 这是我的控制器: @RestController @RequestMapping(“/api/user”) public class UserController { @RequestMapping(“/”) public UserDto user(@AuthenticationPrincipal(errorOnInvalidType = true) User user) { return UserDto.fromUser(user); } } 这是我自定义的User类: @Entity() @Table(name = “app_user”) public class User extends AbstractEntity implements Serializable { // —————————— FIELDS —————————— @NotNull @Column(unique = true) @Pattern(regexp = “[a-zA-Z_\\-\\.0-9]+”) @Size(min = 1, max = 20) private String […]

如何在Spring中的RESTful Web应用程序中进行身份validation(登录/注销)?

我想通过身份validation http://myhost/login?user=…&password=… 并注销 http://myhost/logout 我正在使用Gradle,Spring Boot和Java配置,因此没有web.xml,没有上下文配置,没有Web表单等等。 在多页和多文件样本上无法摆脱谷歌噪音……

如何在春季mvcrest服务?

我正在尝试制作简单的rest服务,这是由移动开发人员使用的每个人使用的示例。所以我需要向每个人发送静态数据。我正在尝试发送静态数据。 { name:”abcd” } 换句话说,如果有人像我这样点击我的系统http://192.168.12.61:8080/springfirst/hello 。那么用户就会超过json。 我这样做是为了制作http://www.programming-free.com/2014/03/spring-mvc-40-restful-web-service-json.html 我按照这一步 下载这些jar文件( – jackson-annotations-xxxjar – jackson-core-xxxjar – jackson-databind-xxxjar)并包含在lib文件夹中。 这是我的代码web.xml Spring MVC Application HelloWeb org.springframework.web.servlet.DispatcherServlet HelloWeb / 你好,servelts.xml controller.js package com.tutorialspoint; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping(“/hello”) public class HelloController{ @RequestMapping( method = RequestMethod.GET,headers=”Accept=application/json”) public String printHello(ModelMap model) { return “abcd”; } }

Spring安全性:添加一个支持IP身份validation的新ROLE

我是Spring Security的新用户。 我之前配置了Spring Security来为我的Web应用程序执行基于表单的身份validation。 我已经使用Annotations来指示哪些控制器/方法需要ROLE_USER与匿名,并实现了我自己的daoAuthenticationProvider来执行身份validation。 我现在正在实现REST服务,需要在BasicAuthentication上进行身份validation+对我的数据库进行IPvalidation。 所以我在REST服务控制器上定义了一个新角色ROLE_IP_AUTH。 在这一点上我只是有点不知所措。 有人能给我一个简短的描述我应该采取的下一步措施吗? 我是否需要为新角色创建新的元素? 我应该切换到这个FilterChainProxy吗? 如果是,我应该包括哪些filter? 我应该在现有的daoAuthenticationProvider类中处理这个问题吗? 基本上我只需要知道我要进入的方向。我想我已经足够了解到达目的地,只有5种不同的方式可以在Spring看到它。 附加信息我当前的实现具有如下配置元素: 我剩下的问题是如何实现自己的IPvalidation。 我可以使用自定义filter添加BasicAuthfilter并实现我自己的基本身份validationfilter来检查IP。 但我对如何使该filter仅适用于我的REST服务使用的ROLE_IP_AUTH模糊不清?

从文件系统导入eclipse项目,然后在服务器上运行

我想在这个链接上做春季安全教程。 在标题为“运行没有Spring Security的教程应用程序”的部分中 ,我采取了以下步骤(针对当前版本与编写教程时使用的版本之间的差异进行了调整): 1.) I downloaded the latest release of the Spring Security Distribution, 2.) found and unzipped a war file in the dist directory called spring-security-samples-tutorial-3.1.2.RELEASE.war 3.) Renamed the resulting folder spring-security-tutorial 4.) Created a general project in eclipse called spring-security-tutorial 5.) Imported all of the contents of the unzipped spring-security-samples-tutorial-3.1.2.RELEASE.war 6.) Right […]

将自定义AuthenticationProcessingFilter与一起使用(auto-config =“true”)

Spring security(2.0.x)http命名空间,表单登录定义自动使用AuthenticationProcessingFilter。 我也知道如果我设置auto-config=”false”我可以通过提供自定义bean定义来自定义身份validation。 我有CustomAuthenticationProcessingFilter扩展AuthenticationProcessingFilter覆盖acquireUsername并使用自定义逻辑来获取用户名而不是传递的用户名。 protected String obtainUsername(HttpServletRequest request) { // custom logic to return username from parameter/cookies/header etc … } 是否可以使用CustomAuthenticationProcessingFilter,同时仍然使用auto-config=”true” 而无需定义customAuthFilter和所有依赖bean? … …

Spring框架:HTTP OPTIONS返回所有METHODS(get,put,post,delete,trace,head,options)

我写了一个spring web应用程序。 除了一件事,它非常可靠。 应用程序返回HTTP OPTIONS请求,其响应为“允许:GET,HEAD,POST,PUT,DELETE,OPTIONS”,但这不是真的,只允许GET和POST。 其他操作(GET和POST除外)返回“操作不受支持”错误405和服务器名称和版本…等。 安全团队不喜欢使用“不支持”错误消息返回服务器的所有详细信息。 我花了很多时间试图覆盖该错误页面或为405引入自定义错误页面……但我没有成功。 我整天都在尝试禁用HTTP OPTIONS。 没有成功。 还有一个细节,我运行curl语句来测试: curl “http://localhost:8080/webappX/welcome.htm” -X PUT -v 我的应用程序返回: HTTP Status 405 – Request method ‘PUT’ not supported Tomcat/6.0…etc 由于此数据(tomcat版本,服务器信息和响应元数据)可能用于安全攻击,如何创建隐藏我的服务器详细信息并提供非常小的错误页面的自定义405页面? 或者我如何禁用PUT,DELETE,TRACE,OPTIONS,HEAD以便tomcat可能只是忽略它们或者不提供有关我的服务器的内部信息。 谢谢

Spring @Secured和@PreAuthorize对普通(非Web)应用程序没有任何影响

我用两个安全的方法创建了一个Application Spring组件: @Component public class Application { public void run() { onlyAdminMethod(); onlyAdminMethod2(); } @Secured( “ROLE_ADMIN” ) public void onlyAdminMethod() { System.out.println( “Admin-only method called” ); } @PreAuthorize( “hasRole(‘ROLE_ADMIN’)” ) public void onlyAdminMethod2() { System.out.println( “Admin-only method 2 called” ); } } 我在该bean上调用run()方法,我从Spring XML上下文中获取: ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext.xml”); context.getBean( Application.class).run(); 没有发生 –即使没有身份validation和SecurityContextHolder.getContext().getAuthentication() 方法也会正常调用 SecurityContextHolder.getContext().getAuthentication()返回null […]

Spring Security OAuth 2,带有表单登录

我正在尝试配置我的Spring启动应用程序以使用表单登录,并使用OAuth 2授权服务器validation凭据(将表单登录中的凭据发送到用户授权URL。 但是,当我使用以下SecurityConfig并且我转到资源时,而不是使用表单登录,它会重定向到授权服务器,询问我的凭据(使用基本身份validation),然后重定向回应用程序本身。 我正在使用以下SecurityConfig : @Configuration @EnableOAuth2Sso public class SecurityConfig extends OAuth2SsoConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http .logout() .and() .antMatcher(“/**”) .authorizeRequests() .anyRequest().authenticated() .and() .csrf() .csrfTokenRepository(csrfTokenRepository()).and() .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class) .formLogin(); } // CSRF repository + filter } 我将formLogin()提供给configure()方法,但这似乎不起作用。 以下配置位于我的application.yml文件中: spring: oauth2: client: clientId: test clientSecret: secret accessTokenUri: http://localhost:8081/uaa/oauth/token userAuthorizationUri: http://localhost:8081/uaa/oauth/authorize clientAuthenticationScheme: header […]

Spring安全登录与restWeb服务

我的公司有一个特定的身份validation提供程序,它是一个内部REST Web服务。 实际上,您为Web服务提供了一个登录名/密码,它返回一个令牌(有效期为几个小时),必须在每个下一个Web服务请求的标题中给出。 我需要创建一个Web应用程序,我需要将其插入此身份validation提供程序。 将它与Spring Security集成的最佳方法是什么? 如何在不要求用户重新登录的情况下管理我的webapp中的令牌过期?