2017-12-21 19 views
-1

스프링 부트, 스프링 시큐리티 4, 타임 라이트 (Thymeleaf)를 사용하려고합니다. 사용자가 "admin"또는 다른 역할을 담당하고 있다면. html 블록이 표시되어야합니다.하지만 이제는 항상 페이지에 표시됩니다. 은 여기 내 html로스프링 부트 보안 - Thymeleaf sec : 작동하지 않음

여기
<html lang="en" xmlns:th="http://www.thymeleaf.org" 
    xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"> 
<div sec:authorize="hasRole('ROLE_GUEST')"> 
    <p class="bg-info">guest</p> 
    </div> 
    <div sec:authorize="hasRole('ROLE_ADMIN')"> 
     <p class="bg-info">you can see this if you have permission to acess role_admin</p> 
    </div> 

그리고 내가 thymeleaf - 엑스트라 - springsecurity4를 추가 할 내 pom.xml 파일이 있습니다. 또한 thymeleaf - 엑스트라 - springsecurity3

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <parent> 
     <groupId>com.zhongdihang.resp</groupId> 
     <artifactId>resp-parent</artifactId> 
     <version>1.0.0</version> 
     <relativePath>../resp-parent</relativePath> 
    </parent> 
    <artifactId>resp-serve</artifactId> 
    <packaging>war</packaging> 
    <name>Real estate sharing platform serve</name> 
    <description>Real estate sharing platform serve</description> 
    <dependencies> 
     <!-- Compile --> 
     <dependency> 
      <groupId>com.zhongdihang.resp</groupId> 
      <artifactId>resp</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.zhongdihang.resp</groupId> 
      <artifactId>resp-test</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-jpa</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-security</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-thymeleaf</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.session</groupId> 
      <artifactId>spring-session-jdbc</artifactId> 
     </dependency> 
     <!-- Optional --> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-configuration-processor</artifactId> 
      <optional>true</optional> 
     </dependency> 
     <!-- Runtime --> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>com.oracle</groupId> 
      <artifactId>ojdbc6</artifactId> 
      <scope>runtime</scope> 
      <version>11.2.0.4</version> 
     </dependency> 
     <dependency> 
      <groupId>com.alibaba</groupId> 
      <artifactId>fastjson</artifactId> 
      <version>1.2.12</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-httpclient</groupId> 
      <artifactId>commons-httpclient</artifactId> 
      <version>3.1</version> 
     </dependency> 
     <dependency> 
      <groupId>com.microsoft.sqlserver</groupId> 
      <artifactId>sqljdbc4</artifactId> 
      <version>4.0</version> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.mybatis</groupId> 
      <artifactId>mybatis</artifactId> 
      <version>3.4.5</version> 
     </dependency> 
     <dependency> 
      <groupId>org.mybatis</groupId> 
      <artifactId>mybatis-spring</artifactId> 
      <version>1.3.1</version> 
     </dependency> 
     <!--mapper --> 
     <dependency> 
      <groupId>net.sf.dozer</groupId> 
      <artifactId>dozer</artifactId> 
      <version>5.4.0</version> 
      <exclusions> 
       <exclusion> 
        <groupId>org.slf4j</groupId> 
        <artifactId>slf4j-api</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>org.slf4j</groupId> 
        <artifactId>jcl-over-slf4j</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>org.slf4j</groupId> 
        <artifactId>slf4j-log4j12</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
     <!-- 
     <dependency> 
      <groupId>org.mybatis.spring.boot</groupId> 
      <artifactId>mybatis-spring-boot-starter</artifactId> 
      <version>1.1.1</version> 
     </dependency> 
     --> 
     <dependency> 
      <groupId>org.thymeleaf.extras</groupId> 
      <artifactId>thymeleaf-extras-springsecurity4</artifactId> 
     </dependency> 
    </dependencies> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
       <executions> 
        <execution> 
         <goals> 
          <goal>repackage</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

그리고 여기 내의 SecurityConfig 나를 도울 수

@Configuration 
    @EnableWebSecurity 
    @EnableGlobalMethodSecurity(prePostEnabled = true) 
    public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private PasswordEncoder passwordEncoder; 

    @Autowired 
    private RoleService roleService; 


    @Autowired 
    private SecurityUserDetailsService userDetailsService; 

    @Bean 
    public DaoAuthenticationProvider daoAuthenticationProvider() { 
     DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); 
     provider.setUserDetailsService(userDetailsService); 
     provider.setPasswordEncoder(passwordEncoder); 
     return provider; 
    } 

    @Value("${" + ApplicationConstants.THIS_APP_CONFIG_PREFIX + ".security.debug:false}") 
    private boolean debug = false; 

    @Autowired 
    public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(userDetailsService); 
     auth.authenticationProvider(daoAuthenticationProvider()); 
    } 

    private void configureExceptionHandling(ExceptionHandlingConfigurer<HttpSecurity> handler) { 
     handler.authenticationEntryPoint(new SecurityAuthenticationEntryPoint()); 
    } 
    private void configureAuthorizeRequests(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry) { 
     //registry.accessDecisionManager(new SecurityAccessDecisionManager());  
     registry.antMatchers("/login/**","/auth/**","/api/open/person/**","/api/booking/**","/api/module/menu","/api/booking").permitAll(); 
     List<RoleEntity> list = roleService.findAll(); 
     for (RoleEntity roleEntity : list) { 
      if(roleEntity.getModule()!=null) { 
       registry.antMatchers(roleEntity.getModule().getPath()+"/**").hasAuthority(roleEntity.getNumber()).anyRequest().authenticated(); 
      } 
     } 
     registry.anyRequest().authenticated(); 
     //registry.anyRequest().hasAnyRole("ADMINISTRATOR"); 
    } 

    private void configureFilter(HttpSecurity http) throws Exception { 
     //http.addFilterBefore(new SecurityAuthorizationFilter(sessionrepo), 
     //UsernamePasswordAuthenticationFilter.class); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.headers().frameOptions().disable(); 
     configureFilter(http); 
     configureExceptionHandling(http.exceptionHandling()); 
     configureAuthorizeRequests(http.authorizeRequests()); 
     http.csrf().disable(); 
     http.formLogin() 
      .loginPage("/login") 
      .usernameParameter("username") 
      .passwordParameter("password") 
      .failureHandler(new SecurityAauthenticationFailureHandler()) 
      .successHandler(new SecurityAuthenticationSuccessHandler()) 
      .permitAll(); 
     http.logout() 
      .logoutUrl("/logout") 
      .logoutSuccessHandler(new SecurityLogoutSuccessHandler()) 
      .permitAll(); 
    } 

    @Override 
    public void configure(WebSecurity web) throws Exception { 
     web.debug(debug); 
     web.ignoring().antMatchers(HttpMethod.OPTIONS); 
     web.ignoring().antMatchers("/assets/**"); 
     web.ignoring().antMatchers("/**.ico"); 
     web.ignoring().antMatchers("/v2/api-docs"); 
    } 
} 

누구는 시도? 고맙습니다 ~

+0

'.하지만 지금은 항상 페이지에 표시됩니다.'그것은'로그인 페이지입니까? – Salman

+0

이 요소는 index.html에 작성되었습니다. 그리고 두 메시지를 볼 수 있습니다. 그러나 사용자에게는 어떤 역할도 없습니다. – StupidPz

+0

'configAuthentication (AuthenticationManagerBuilder auth)'메소드를 찾을 수 없습니다. 'jdbcAuthentication'이 당신을 수용 할만한 해결책입니까? – Salman

답변

1

어젯밤에 내 매니저에게 물었습니다.이 문제를 해결하는 데 도움이되었습니다. 나는 3.0.9.RELEASE thymeleaf springboot 1.5.8.RELEASE을 사용하고, 그래서 난 당신이 pom으로에

 <dependency> 
      <groupId>org.thymeleaf.extras</groupId> 
      <artifactId>thymeleaf-extras-springsecurity4</artifactId> 
      <version>3.0.2.RELEASE</version> 
     </dependency> 

를 추가하려고 .SO org.thymeleaf.extras 최신을 사용해야합니다.