我正在处理这个问题,从7个小时前开始,我找不到一个解释,为了简单起见,我只是把例子做得小一点。我需要一些安全访问(JWT)的网址,和一个表单登录的其他路径(仪表板)。
这是我的代码:
@EnableWebSecurity
public class MultiHttpSecurityConfig {
@Autowired
private UserDetailsService jwtUserDetailsService;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(jwtUserDetailsService)
.passwordEncoder(passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder() {
return NoOpPasswordEncoder.getInstance();
}
@Configuration
@Order(1)
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Autowired
private JwtRequestFilter jwtRequestFilter;
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
// Get Request and /Authenticate do not need authentication
.authorizeRequests()
.antMatchers("/authenticate", "/authenticate/**").permitAll()
.antMatchers(HttpMethod.GET, "/api/**").permitAll()
// all others do need authentication
.anyRequest().authenticated()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
@Configuration
public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/dashboard/index.html").authenticated()
.and()
.formLogin();
}
}
此示例有效,JWT 机制效果很好。它唯一不起作用的是表单登录。当我点击浏览器 localhost:8080/dashboard/index.html
时,该文件出现。
这就是我需要的:
/授权,授权--
/api接口--
/api接口--
/dashboard/index.html-
我知道anyRequest().authenticated()
,它在第一个配置中,但如果我什至评论该行,则完全忽略了第二个顺序
。
我应该添加或删除什么来完成我的想法?
在您的FormLoginWebSecurityConfigrerAdapter
中,antMatcher()
应该在授权请求()
之前调用-这表明此过滤器链仅将请求应用于/dashboard/index. html
。
http.antMatcher("/dashboard/index.html")
.authorizeRequests()
.anyRequest().authenticated() // since this filter chain only apply to /dashboard/index.html, don't need use antMatchers() to check again
.and()
.formLogin();
更多信息: https://docs.spring.io/spring-security/site/docs/current/reference/html5/#multiple-httpsecurity
第二个问题是FormLoginWebSecurityConfigurerAdapter
的顺序必须早于(小于)ApiWebSecurityConfigurationAdapter
WebSecurityConfigurerAdapter
的默认@Order为100
,因此您应该在FormLoginWebSecurityConfigurerAdapter
上注释@Order(0)
。
我正在开发一个基于spring security的web应用程序,我想根据登录用户的角色是ADMIN还是user来限制侧栏上的项目。就身份验证而言,一切正常,但角色没有按预期工作。 跟随这篇文章 例如- 即使我以-ADMIN登录,上述元素也永远不可见。 有人能在这里帮助我理解出了什么问题吗? 安全配置 UserDetailsImpl 用户详细信息服务 用户详细信息 控制器 使用者 在db中创建的表
我在我的Spring Boot应用程序中使用Spring Security,似乎Thymeleaf授权无法正常工作。 我有一个带有以下代码的Thymeleaf模板: 举例如下:https://github.com/thymeleaf/thymeleaf-extras-springsecurity 但是,显示的唯一内容是和,并且无论用户的角色如何,授权始终被忽略。 我的胸腺配置是: 我对使用以下依赖
我正在开发一个带有Angular2前端的RESTful Spring后端。我将访问令牌(JWT实现)存储在cookie中。为了保护自己免受对post请求的XSRF攻击,我需要在所有页面上启用XSRF保护,登录页面除外。根据这里的Spring Security指南,我启用了。 但是,当我访问公共API()时,没有设置XSRF-TOKEN。此外,当我从Angular2提交我的登录表单数据时,系统提示一
我在使用Spring Security&Thymeleaf时遇到了一个问题,特别是在尝试使用hasRole表达式时。admin“用户有一个角色”admin“,但在尝试时解析为false 我得HTML: 结果如下: 我已经在security.xml文件中启用了use-expressions 并且在我的配置中包含了SpringSecurityDiscuale 而具有一组 为什么不起作用? 我很感激你的
编辑: 我进一步深入研究了这个问题,发现即使使用单一配置,问题仍然存在。如果我使用单一配置并保持 网址不安全。删除antMatcher和antMatcher会立即保护网址。也就是说,如果我使用: 那么,只有Spring Security性可以保护url。为什么antMatcher不能正常工作? (更新了标题以包含实际发行。) 原文: 我提出了以下问题: > Spring REST安全性-以不同的方
我在我写的index.xhtml页面中使用了Spring4和Thymeleaf: 我的build.gradle(一些依赖项)是: 我的spring-security.xml是: 以前,我使用Apache瓷砖,因为它都工作得很好。我不明白为什么Spring Secury不能和Thymeleaf一起工作。求求你,救命!
我已经看了下面的帖子。我仍然无法理解重定向URI的概念。 https://www.baeldung.com/spring-webclient-oauth2 https://docs.spring.io/spring-security/site/docs/5.0.7.RELEASE/reference/html/oauth2login-advanced.html#oauth2login-高级重定向终
这是Grails Spring Core Security插件的 /not/副本-无法解析类。 我正在将一些代码从grails 2.2.4升级到grails 2.4.4(java 7)并使用Spring Security插件。 在2.2.4下工作的导入在2.4.4下开始不工作,我去寻找答案。 我已经在下面的链接中找到了答案 Grails Spring核心安全插件-无法解析类,已更改 导入grail