【问题标题】:Why thymleaf can't read this expression : "${#authorization.expression('hasRole(''ROLE_ADMIN'')')}为什么 thymleaf 无法读取此表达式:“${#authorization.expression('hasRole(''ROLE_ADMIN'')')}
【发布时间】:2018-09-02 02:24:48
【问题描述】:

我试图获取角色名称(即:ROLE_ADMIN、ROLE_USER)来分配角色的视图。我用了这段代码

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    private DataSource dataSource;

    @Value("${spring.queries.users-query}")
    private String usersQuery;

    @Value("${spring.queries.roles-query}")
    private String rolesQuery;


    @Autowired
    public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
        auth.
        jdbcAuthentication()
            .dataSource(dataSource)
            .usersByUsernameQuery(usersQuery)
            .authoritiesByUsernameQuery(rolesQuery)
            .passwordEncoder(bCryptPasswordEncoder);



    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.
            authorizeRequests()
                .antMatchers("/","/login").permitAll()
                .antMatchers("/users","/roles").access("hasRole('ROLE_ADMIN')").anyRequest()
                .authenticated()
                .and()
                .csrf()
                .disable()
            .formLogin()
                .loginPage("/login").failureUrl("/login")
                .defaultSuccessUrl("/projects")
                .usernameParameter("username")
                .passwordParameter("password")
                .and()
            .logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/login").and().exceptionHandling()
                .accessDeniedPage("/access-denied");

    }

     @Override
        public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/webjars/**");
        web.ignoring().antMatchers("/css/**","/fonts/**","/libs/**");
        }



<div th:if="${#authorization?.expression('hasRole(''ROLE_ADMIN'')')}">
            <ul class="nav navbar-nav" >
                <li><a href="/users">Usuarios</a></li>

                <li><a href="/roles">Roles</a></li>

                <li><a href="/professionalExperiences">Experiencia <br />Profesional
                </a></li>

                <li><a href="/professinal_skills">Professional <br />Skills
                </a></li>

                <li><a href="/personal_skills">Personal <br />Skills
                </a></li>

                <li><a href="/projects">Proyectos</a></li>

                <li><a href="/academics">Formacion<br /> Academica
                </a></li>

                <li><a href="/certifications">Cartificaciones</a></li>

                <li><a href="/courses">Cursos</a></li>

                <li><a href="/language_evals">Evaluacion<br /> del idioma
                </a></li>

            </ul>
            </div>

当我在尝试登录后运行项目时,我得到了这个错误: EL1011E: 方法调用:尝试在空上下文对象上调用方法表达式(java.lang.String) 我加了一个? #authorization 之后但同样的问题 我正在使用:Spring Boot、thymleaf、Maven、

【问题讨论】:

    标签: spring-boot spring-security thymeleaf


    【解决方案1】:

    您可以尝试以下方法吗?

    <div sec:authorize="hasRole('ROLE_ADMIN')">
    

    【讨论】:

    • 有了这个我可以访问但无法控制是否是 ROLE_ADMIN 或 ROLE_USER :
    • 您确定您的用户没有两个角色或角色没有分层定义吗?您可以对只有 ROLE_USER 的用户进行测试吗?
    • 角色在 WebSecurityConfig 中配置: .antMatchers("/","/login").permitAll() .antMatchers("/users","/roles").access("hasRole( 'ROLE_ADMIN')").anyRequest() .authenticated() .and() .... 等我确定用户只有一个角色
    • 你在哪里给每个用户一个角色?另外,您能否尝试在您的页面中添加以下 div 并让我知道输出是什么
      登录用户:跨度> |角色:
    • 结果是:登录用户:| Roles:,我一直认为spring可以从WebSecurityConf获取角色
    猜你喜欢
    • 2020-06-12
    • 2020-06-26
    • 1970-01-01
    • 2015-01-12
    • 2017-02-04
    • 1970-01-01
    • 2020-06-15
    • 2019-02-13
    • 2020-04-28
    相关资源
    最近更新 更多