对我有弹簧安全在我的应用确保了一些路径,并把其他人打开匿名访问。我遇到的问题与我将访问权限保留为“permitAll”的开放部分相关。我只想保护特定的路径免受非ADMIN的访问,但我希望管理员用户在路径的开放部分时能够被识别。春季安全主体空/用户没有登录permitAll路径
Thymeleaf模板(部分):
<p>Hello Spring Boot User <span th:text="${username}"/>!</p>
<div sec:authorize="isAnonymous()">isAnonymous</div>
<div sec:authorize="isRememberMe()">isRememberMe</div>
<div sec:authorize="isAuthenticated()">isAuthenticated</div>
<div sec:authorize="isFullyAuthenticated()">isFullyAuthenticated</div>
注:该模型的用户名定义为:
String username = (principal != null ? principal.getName() : "ANONYMOUS");
配置(基于Java) - 有正在使用多种类型的认证的
@Configuration
public static class FormLoginConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/form/**").authorizeRequests().anyRequest().authenticated()
.and().formLogin().permitAll().loginPage("/form/login").loginProcessingUrl("/form/login")
.and().logout().logoutUrl("/form/logout").invalidateHttpSession(true).logoutSuccessUrl("/");
}
}
@Order(45) // LOW
@Configuration
public static class BasicAuthConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/basic/**").authorizeRequests().anyRequest().authenticated()
.and().httpBasic();
}
}
没有SECURI ty配置/打开路径。只是一个控制器。/form路径还有一个控制器。这些都像这样(只是路径现在不同):
@Controller
@RequestMapping("/open")
public class OpenController extends BaseController {
@RequestMapping({"", "/"})
public String home(HttpServletRequest req, Principal principal, Model model) {
commonModelPopulate(req, principal, model);
return "home"; // name of the template
}
}
如果我去这个路径/开放(未保护的)我看到:
Hello Spring Boot User ANONYMOUS!
但是,如果我去这条道路/形式(表单登录保护 - 登录后)我看到:
Hello Spring Boot User admin!
isAuthenticated
isFullyAuthenticated
所以我觉得可能有多个问题 这里。首先是thymeleaf sec:授权属性没有做任何事情,其次是我似乎只能访问委托人和其他安全信息,如果我在受保护的路径下。
是否有保护只是一个路径(和子路径),但允许在我的应用程序在其他地方访问的主要数据和安全数据的方法吗?
我偶然发现,通过一些试验和错误此解决方案,但它是伟大的,有这样的验证。谢谢! –