我已按照以下说明为管理员和用户创建两个不同的http安全块。docs.spring.io/spring-security-multiple-httpsecurity
正如文档所说,如果URL不是以/aaa开头,将使用另一个配置来进行模式化。
但当我将@Order(1)放在管理块时,管理页面工作正常,用户页面不会重定向到登录页面/login/user
当我把@Order(1)放在用户块,用户页面工作正常时,管理页面也不会重定向到登录页面/登录/管理员。
这是我的Java代码
@EnableWebSecurity
public class MultiHttpSecurityConfig {
/**
* intercept user url
*/
@Configuration
@Order(1)
public static class UserWebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
CustomAuthenticationSuccessHandler successHandler;
@Autowired
CustomAuthenticationFailureHandler failureHandler;
@Autowired
private CustomAuthenticationProvider customAuthProvider;
@Autowired
private CustomUserDetailsService userDetailsService;
@Value("${my.cookie.timeout}")
private int cookieTimeOut;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/css/**", "/js/**", "/images/**, /fonts/**").permitAll()
.antMatchers("/bbb/**","/aaaa/**").hasAnyRole("USER");
http.formLogin()
.successHandler(successHandler)
.failureHandler(failureHandler)
.loginPage("/login/user").permitAll();
http.logout().permitAll();
http.rememberMe().key("uniqueAndSecret").tokenValiditySeconds(cookieTimeOut);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(customAuthProvider);
auth.userDetailsService(userDetailsService);
}
}
/**
* intercept admin url
*/
@Configuration
public static class AdminWebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
CustomAuthenticationSuccessHandler successHandler;
@Autowired
CustomAuthenticationFailureHandler failureHandler;
@Value("${my.cookie.timeout}")
private int cookieTimeOut;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/css/**", "/js/**", "/images/**, /fonts/**").permitAll()
.antMatchers("/ccc/**","/dddd").hasAnyRole("ADMIN");
http.formLogin()
.successHandler(successHandler)
.failureHandler(failureHandler)
.loginPage("/login/admin").permitAll();
http.logout().permitAll();
http.rememberMe().key("uniqueAndSecret").tokenValiditySeconds(cookieTimeOut);
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("test").password("test").roles("ADMIN");
}
}
}
正如dur下面所说,关键原因是授权请求()方法匹配订单(1)中的所有网址,所以我首先需要在授权请求()之前添加antMatcher("/bbb/*")** 。
但antMatcher()只匹配一种url,如果我还有一种url要匹配,例如“/bb/”、“/aaa/*”,如何实现这一点?然后我需要再添加一个WebSecurityConfigurerAdapter配置?有没有更好的方法来减少代码?
我在spring-security SDK request estMatcher()方法中找到了解决方案,它提供了一个示例在request estMatcher()方法之上。
下面是我在Order(1)中匹配用户URL的代码
http.csrf().disable();
http.requestMatchers()
.antMatchers("/bbb/**", "/aaa/**")
.and()
.authorizeRequests()
.antMatchers("/**").hasAnyRole("USER");
http.formLogin()
.successHandler(successHandler)
.failureHandler(failureHandler)
.loginPage("/login/user").permitAll();
http.logout().permitAll();
然后bbb和aaa都已匹配,不需要创建其他配置
但另一个问题发生了,它会显示“405方法不允许”时,张贴用户名和密码登录/用户界面在用户登录页面,而管理页面工作正常
我已经搜索了谷歌,它告诉禁用csrf,但我已经禁用csrf...
在我的一个项目中,我没有使用表单登录,而是实现了一个自定义的AccessDeniedHandler和AuthenticationEntryPoint,它们可以使用一些我需要的自定义逻辑重定向到不同的登录页面。不过loginPage()最后也是一个AuthenticationEntryPoint。
它们可以通过以下方式添加:
.exceptionHandling().authenticationEntryPoint(new YourCustomAuthEntryHandler()).and()
.exceptionHandling().accessDeniedHandler(new YourCustomAccessDeniedHandler())
这只是一个想法,也许值得检查一下。
此外,我认为您将需要对所有请求进行身份验证,在 authorizeRequests() 块中为两个配置添加此行:
.anyRequest().authenticated()
问题内容: include(‘header.php’); 上面的代码不断给我重定向的问题。错误如下: 警告:无法修改标头信息-已在/ Applications / MAMP / htdocs / testygubbins / OO / test / form中发送的标头(输出始于/Applications/MAMP/htdocs/testygubbins/OO/test/header.php:15
我们可以配置多个HttpSecurity实例,就像我们可以有多个块抑郁。关键在于对WebSecurityConfigurationAdapter进行多次扩展。例如下面是一个对/api/开头的URL进行的不同的设置。 @EnableWebSecurity public class MultiHttpSecurityConfig { @Autowired public void con
我是JSTL新手,由于某种原因,我无法让测试线工作。以下是我使用的简化代码: 当我使用 ${hasChild} 它在屏幕上打印真实,但在测试线上不会发出嗡嗡声,我不知道为什么。有人能帮忙吗?
问题内容: 检查这样的情况后,我有一条重定向的路线 条件为真但未安装组件时,URL会更改。其余的组件代码如下。 我的应用程序组件包含在BrowserRouter中,例如thi 当我在浏览器中直接命中url时,例如’localhost:3000 / intro’组件已成功安装,但是当它通过重定向时,它不会显示该组件。我如何解决它? 因此缺少一个细节,我尝试创建另一个项目来重现该问题。我的App组件是
然而,我发现实际的构建顺序是不同的...build之后,实际的订单看起来像; 在这种情况下,Maven从哪里获取构建命令?
问题内容: 下面的代码有任何错误吗?以下代码无法提供多个目录服务。当我访问localhost:9090 / ide时,服务器将返回404错误。 当我这样更改代码时, 它会按我预期的那样工作。 问题答案: 使用中的问题之一是请求路径用于构建文件名,因此,如果要从根目录以外的任何地方提供服务,则需要剥离到该处理程序的路由前缀。 标准库为此提供了一个有用的工具,但该工具只能在s上使用,而不能在s 上使用
我们在联系人列表中有一组表示不同人的对象。我们有一个 lookUpProfile 函数,它将 name 作为参数。该函数应检查姓名是否为实际联系人的名字。它将在控制台上打印联系人姓名,如果姓名与联系人的名字匹配,则为 true,否则,它将打印 false。 我想让我的while循环遍历数组,直到nameCheck等于true或者我的长度大于contact length(也就是它到达数组的末尾)。
问题内容: 在Python 3中,使用多个键按字典顺序对对象列表进行排序非常容易。例如: 该参数使您可以指定是升序还是降序。但是,如果要按多个键进行排序,但是要对第一个键使用降序排序,而对第二个键使用升序排序,该怎么办? 例如,假设我们有一个具有两个属性的对象,而,其中an和is是。我们希望通过梳理这些对象的名单在 递减 顺序(使之与点的数量最多的对象是第一位的),但与同等数量的对象,我们要排序这