Tag: spring security

在需要身份validation的控制器上运行unit testing

我有一个spring boot应用程序,需要登录才能执行某些操作。 我试图使用MockMvc测试它们,但它似乎不起作用。 我一直收到状态为403的HTTP响应(禁止)。 可能是身份validation部分出了问题。 我已经尝试过遵循文档 ,但我无法使其正常工作。 这是我目前的测试代码: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = {Application.class}) @WebIntegrationTest(“server.port = 8093”) public class PasswordChangeTests { @Autowired private EmbeddedWebApplicationContext webApplicationContext; @Autowired private UserRepository userRepository; private MockMvc mockMvc; @Before public void setUp() throws Exception { this.mockMvc = MockMvcBuilders .webAppContextSetup(webApplicationContext) .apply(springSecurity()) .build(); } @Test public void changePasswordWorks() throws Exception { // Send password change […]

没有名为authenticationManager的bean

可能重复: 获取错误org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为’springSecurityFilterChain’的bean 在我的Spring应用程序中,我不断收到此错误: No bean named ‘org.springframework.security.authenticationManager’ is defined: Did you forget to add a gobal element to your configuration (with child elements)? Alternatively you can use the authentication-manager-ref attribute on your and elements. 在我的Spring Security上下文xml文件中,我定义了以下内容: 当我明确定义了我的身份validation管理器和身份validation提供程序时,有什么想法让它抱怨吗? 注意:这可能会有所帮助,这是一个更具描述性的错误: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘org.springframework.security.filterChains’: Cannot resolve reference to bean ‘org.springframework.security.web.DefaultSecurityFilterChain#2’ while setting bean property […]

自定义登录表单。 配置Spring安全性以获取JSON响应

我有一个简单的应用程序,分为两部分: 一个后端,它使用Spring-boot / Spring-security公开REST服务 一个只包含静态文件的前端。 这些请求由侦听端口80的nginx服务器接收。 如果请求URL以/ api /开头,请求将重定向到后端。 否则,请求由提供静态文件的nginx处理。 我创建了一个自定义登录表单(在前端部分),我正在尝试配置Spring-boot服务器。 有很多例子我可以看到如何定义“登录成功”url和“登录错误”url,但我不希望Spring-security重定向用户。 如果登录成功或HTTP 40x登录失败,我希望Spring-security使用HTTP 200回答。 换句话说:我希望后端只回答JSON,而不是HTML。 到目前为止,当我提交登录表单时,请求被重定向,我得到默认的Spring登录表单作为答案。 我尝试使用.formLogin().loginProcessingUrl(“/login”); 而不是loginPage(“”) : @Configuration @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) protected static class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser(“user”).password(“password”).roles(“ADMIN”); } @Override public void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .anyRequest().authenticated() .and() .formLogin() .loginProcessingUrl(“/login”);

使用Spring Security标记库时将exception映射到404页面

将exception映射到404页面时,Spring Security标记无法从安全上下文中查找身份validation信息。 使用“真实”404,可以找到认证。 我的web.xml: com.example.NotFoundException /app/404 404 /app/404 在JSP上我有: /app/404路径映射到只返回视图的控制器。 当我浏览到/foo/some_invalid_id , NotFoundException会从控制器中抛出,最后当它进入JSP时,它无法在SecurityContext找到身份validation并呈现这两个选项中的任何一个。 相反,当我浏览/something_that_really_doesnt_exist它能够判断我是否登录并呈现正确的HTML。

Spring 3安全性j_spring_security_check

我正在努力学习Spring安全性如何工作,所以我下载了一些示例项目,然后我尝试将该解决方案实现到我的项目中。 但是当我尝试登录时,我收到404错误,在地址栏中我有http://localhost:8080/fit/j_spring_security_check 。 我试着在这里查看类似的问题,但我无法意识到,如何将它应用到我的项目中。 如果有经验丰富的人可以帮助我,我会非常感激。 我的app结构如下所示: applicationContext.xml中: 的applicationContext-web.xml中: 的applicationContext-security.xml文件:

Spring 3安全性:未调用AccessDeniedHandler

我有一个spring 3应用程序,其配置如下。 当任何用户尝试访问某个页面并且他/她未登录时,我会收到一个带有丑陋堆栈跟踪的Access is Deniedexception。 如何处理此exception并且不允许它转储堆栈跟踪。 我实现了自己的访问被拒绝处理程序但不会被调用。 根据所请求资源的类型,我想显示自定义错误消息或页面。 这是我的弹簧配置。 如何让Spring调用我的访问被拒绝处理程序。 这是我的弹簧配置 下面给出了处理此exception的自定义类 public class AccessDeniedExceptionHandler implements AccessDeniedHandler { private String errorPage; @Override public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException arg2) throws IOException, ServletException { response.sendRedirect(errorPage); } public void setErrorPage(String errorPage) { if ((errorPage != null) && !errorPage.startsWith(“/”)) { throw new IllegalArgumentException(“errorPage must begin with ‘/'”); […]

无法让SHA-256哈希与我的Spring安全性一起工作

我正在使用Spring Roo框架,它使用Spring Security作为安全框架。 我按以下方式配置它: <!– –> 为了使密码匹配,我编辑了UserController create方法,因此使用SHA-256哈希存储密码,与在安全配置文件applicationContext-security.xml配置的相同。 我是这样做的: public String create(@Valid User user, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) throws NoSuchAlgorithmException, UnsupportedEncodingException { if (bindingResult.hasErrors()) { populateEditForm(uiModel, usuario); return “security/users/create”; } MessageDigest md = MessageDigest.getInstance(“SHA-256”); md.update(user.getPassword().getBytes(“UTF-8”)); byte[] digest = md.digest(); usuario.setPassword( new String(digest, “UTF-8”)); uiModel.asMap().clear(); user.persist(); return “redirect:/security/users/” + encodeUrlPathSegment(usuario.getId().toString(), httpServletRequest); } 我尝试将密码设置为admin ,这与默认用户提供的相同:admin,密码:admin配置文件,以检查我的create方法生成的密码是否匹配。 […]

Spring安全性自定义AuthenticationException消息

嗨我需要在Spring安全登录表单中添加一个新的exception,除了我想要自己的错误消息之外,一切都很完美(直到现在它显示“错误的登录/密码”)。 我从用户名密码validationfilter覆盖默认尝试validation方法: @Override public Authentication attemptAuthentication(final HttpServletRequest request, final HttpServletResponse response) { if (!myTest()) { throw new CustomAuthenticationException(“custom message”); } } 我的例外: public class CustomAuthenticationException extends AuthenticationException { public CustomAuthenticationException(final String message) { super(message); } public CustomAuthenticationException(final String message, final Throwable cause) { super(message, cause); } } 在我的控制器中,我在SPRING_SECURITY_LAST_EXCEPTION下看到了我的exception,但错误消息始终是来自坏凭证的消息,我怎么能改变它? 谢谢

使用JDBC和JWT实现Spring OAuth2,并使用基于XML的配置自定义现有授权流

我开始使用Spring OAuth2,在这个过程中,我很难找到相关的教程和内容,主要是因为以下内容 我不想使用Spring Boot 我不想使用Java配置而是使用xml配置 我需要根据我们的特定需求自定义Spring OAuth2授权流程和其他function 我需要禁用一些授权流程 我需要Spring OAuth2来使用自定义用户和角色 我需要在自定义数据库选项卡中使用store oauth_client详细信息 其他的东西 我设法编写了我的实现,解决了上述问题,现在我想分享我的发现,以便拯救他人的痛苦。 请参阅我的答案,了解我所采用的方法,如果您有任何建议,请随时分享您的建议,建议和反馈。 这个问题的主要目标是 获得有关我遵循的方法的反馈,建议和建议 分享我所学到的所有困难,希望能够拯救他人的麻烦,并回馈社区从社区中学到的东西。

在Spring Boot中配置0-legged OAuth 1.0

我想设置一个带有0-legged(因此没有请求或访问令牌)OAuth 1.0的Spring启动应用程序。 我一直在寻找一个例子,我一直在努力寻找如何使用新风格(没有xml)配置东西。 现在我只想得到一个简单的用例,其中只有1个路径(/ oauth)受OAuth保护(其他一切都只是大开),并且它使用自定义ConsumerDetailsS​​ervice (请参阅下面的代码的简单版本)。 这是我的WebSecurityConfigurerAdapter (我的Application.java旁边的SecurityConfiguration.java,我认为这是在spring启动应用程序中配置这种东西的正确方法)。 我很确定我错过了提供程序配置(如http://projects.spring.io/spring-security-oauth/docs/oauth1.html中提到的那样),但我的反复试验并没有产生结果。 @Configuration @EnableWebMvcSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { // 0-Legged OAuth on the /oauth and /lti paths only http.requestMatchers().antMatchers(“/oauth”); // .and()…. what? // ??? something must be missing here – provider? } } 我在我的maven pom.xml中也有这个: […]