我在使用Spring Security && Thymeleaf时遇到问题,特别是在尝试使用hasRole表达式时.'admin'用户有一个'ADMIN'角色,但hasRole('ADMIN')无论如何我会尝试将其解析为false
我的HTML:
1.<div sec:authentication="name"></div> <!-- works fine -->
2.<div sec:authentication="principal.authorities"></div> <!-- works fine -->
3.<div sec:authorize="isAuthenticated()" >true</div> <!-- works fine -->
4.<span th:text="${#authorization.expression('isAuthenticated()')}"></span> <!-- works fine -->
5.<div th:text="${#vars.role_admin}"></div> <!--Works fine -->
6.<div sec:authorize="${hasRole('ADMIN')}" > IS ADMIN </div> <!-- Doesnt work -->
7.<div sec:authorize="${hasRole(#vars.role_admin)}" > IS ADMIN </div> <!-- Doesnt work -->
8.<div th:text="${#authorization.expression('hasRole(''ADMIN'')')} "></div> <!-- Doesnt work -->
9.<div th:text="${#authorization.expression('hasRole(#vars.role_admin)')}"></div> <!-- Doesnt work -->
Run Code Online (Sandbox Code Playgroud)
结果是:
1.admin
2.[ADMIN]
3.true
4.true
5.ADMIN
6."prints nothing …Run Code Online (Sandbox Code Playgroud) 我已从数据库中为当前用户加载了角色.我可以在jsp中使用spring security表达式访问用户角色,并且可以隐藏未经hasRole授权的选项和URL.现在我想将它放在servlet中并将其显示在日志中.(或存储在用户对象会话中).我们怎样才能实现它?
当我使用安全性登录时,我无法使用该request.isUserInRole()方法.我认为没有设置用户的角色.
这是我的安全配置:
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled=true)
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Autowired
private UserDetailsServiceImplementation userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/signup").permitAll()
.antMatchers("/").permitAll()
//.antMatchers("/first").hasAuthority("Service_Center")
.antMatchers("/login").permitAll()
.anyRequest().fullyAuthenticated()
.and().formLogin()
.loginPage("/login")
.usernameParameter("email")
.passwordParameter("password")
.defaultSuccessUrl("/default")
.failureUrl("/login?error").permitAll()
.and().logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/login?logout")
.deleteCookies("JSESSIONID")
.invalidateHttpSession(true).permitAll();
}
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth)
throws Exception {
auth.userDetailsService(userDetailsService);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的User实体:
@Entity
@Table(name="user")
public class User implements Serializable{
/**
*
*/
private static final long …Run Code Online (Sandbox Code Playgroud) 在Spring拦截url配置中ROLE_USER和ROLE_ANONYMOUS之间有什么区别,例如下面的示例?
<http auto-config="false" access-decision-manager-ref="accessDecisionManager"
use-expressions="true">
<intercept-url pattern="/admin/**" access="hasRole('ROLE_ANONYMOUS')"
requires-channel="http" />
<intercept-url pattern="/login/**" access="hasRole('ROLE_ANONYMOUS')"
requires-channel="${application.secureChannel}" />
<intercept-url pattern="/error/**" access="hasRole('ROLE_ANONYMOUS')"
requires-channel="http" />
<intercept-url pattern="/register/**" access="hasRole('ROLE_ANONYMOUS')"
requires-channel="${application.secureChannel}" />
<intercept-url pattern="/" access="hasRole('ROLE_ANONYMOUS')"
requires-channel="http" />
<intercept-url pattern="/**" access="hasRole('ROLE_USER')"
requires-channel="http" />
<form-login login-page="/login" login-processing-url="/login/submit"
authentication-failure-url="/login/error" />
<logout logout-url="/logout" />
</http>
Run Code Online (Sandbox Code Playgroud) 我是 spring mvc 的新手,在我现有的项目中,有一个管理员,他们有权更新数据,但现在我需要创建 2 个新管理员,admin1 和 admin2,它们在登录时只能看到有限的页面,例如:
当管理员登录时,他们可以在菜单栏中看到添加数据、更新数据、发布消息页面。但在 Admin1 的情况下,只能在菜单栏中看到 Post meassage 页面。
所以,请指导我如何在 spring mvc 中完成这项任务 提前致谢。
假设我有一个用户具有以下身份验证:
List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
grantedAuthorities.add(new SimpleGrantedAuthority("READ_PRODUCT"));
grantedAuthorities.add(new SimpleGrantedAuthority("WRITE_PRODUCT"));
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken("usr", "pwd", grantedAuthorities));
Run Code Online (Sandbox Code Playgroud)
在安全检查中,我应该检查用户是否具有访问 API 的正确权限。我做了以下事情来实现它:
http
.httpBasic().disable()
.authorizeRequests()
.antMatchers(HttpMethod.GET, "/product/**").hasAuthority("READ_PRODUCT");
Run Code Online (Sandbox Code Playgroud)
这里我hasAuthority()用来检查用户是否有正确的权限,但是我发现还有一个方法被调用了hasRole()但是我不知道这两个方法的区别是什么?谁能解释我的区别,如果我想在hasRole()这里使用,我如何在这里使用它?我试图替换hasAuthority()为hasRole()但没有成功