将Spring Boot 1.5升级到2 <sec:authorize>不起作用

时间:2019-01-03 17:23:33

标签: spring spring-boot spring-security thymeleaf

我一直在升级我的应用程序以使用Spring Boot 2,但我的视图未正确呈现。他们满足于的内容将不再起作用。我的方法和页面仍然受到适当保护,因此呈现页面似乎是一个问题。另外,isAuthenticated和isAnonymous也不起作用。

我尝试将安全性标签从xmlns:sec =“ http://www.thymeleaf.org更改为xmlns:sec =” http://www.thymeleaf.org/extras/spring-security“ / thymeleaf-extras-springsecurity4“

安全配置

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

@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;

@Autowired
private DataSource dataSource;

@Autowired
private CustomAccessDenied accessDeniedHandler;

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

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

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.jdbcAuthentication().usersByUsernameQuery(usersQuery).authoritiesByUsernameQuery(rolesQuery).dataSource(dataSource).passwordEncoder(bCryptPasswordEncoder);
}

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

    http.csrf().disable()
            .authorizeRequests()
                .antMatchers("/" , "/home").permitAll()
                .antMatchers("/admin/**").hasAnyRole("ADMIN, OWNER")
                .antMatchers("/register/**").hasAnyRole("ADMIN, CASHIER")
                .antMatchers("/staff/**").authenticated()
                .anyRequest().authenticated()
            .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
                .logout()
                    .invalidateHttpSession(true)
                    .clearAuthentication(true)
                    .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                    .logoutSuccessUrl("/")
                    .permitAll()
                .and()
                .headers()
                .frameOptions().disable()
                .and()
                .exceptionHandling()
                    .accessDeniedHandler(accessDeniedHandler);
}

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/pics/**", "/fonts/**");
}
}

HTML页面

    <!DOCTYPE HTML>
    <html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:th="http://www.thymeleaf.org" 
    xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
    <head>
    <title>Home</title>
    <div th:replace="fragments/css"></div>

    </head>
    <body>
    <div th:replace="fragments/header"></div>
    <main>
    <div class="scale-transition scale-out" sec:authorize="isAnonymous()">
        <!-- USER NOT LOGGED IN MENU -->
        <div class="row" style="margin-top: 25px">
            <div class="col s12 m8 offset-m2">
                <form id="idcards">
                            <h1 class="center-align">SWIPE YOUR CARD TO LOGIN</h1>
               <h4 class="center-align">TAP GREY BOX IF NOT WORKING</h4>
                    <input class="center-align grey lighten-3" style="height: 100px; font-size: 60px" id="cardData" type='password' value='' autofocus>
                    <input class="hide" type="button" value="Fill fields" id="filler2" onClick="fillValuesInTextBoxes()">
                </form>
            </div>
            <div class="row">
                <div class="col s12 m8 offset-m2" style="margin-top: 50px">
                    <h3 class="center-align" style="text-decoration: underline;">ANNOUNCEMENTS</h3>
                    <div>
                        <div class="card-panel col s12 m4" th:each="announcementsList: ${announcementsList}">
                            <p class="col s12 m10 offset-m1" th:text="${announcementsList.text}"></p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    </main>
    <div th:replace="fragments/footer"></div>
</body>
</html>

依赖

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <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.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.webflow</groupId>
        <artifactId>spring-webflow</artifactId>
        <version>2.4.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>27.0.1-jre</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-jpamodelgen</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        <version>3.0.4.RELEASE</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <compilerArgument>-proc:none</compilerArgument>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.bsc.maven</groupId>
            <artifactId>maven-processor-plugin</artifactId>
            <executions>
                <execution>
                    <id>process</id>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <phase>generate-sources</phase>
                    <configuration>
                        <!-- source output directory -->
                        <outputDirectory>target/metamodel</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>target/metamodel</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

2 个答案:

答案 0 :(得分:0)

替换了

ExceptionFilter

使用

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

答案 1 :(得分:0)

总是可以通过添加缺少的依赖项或更改所使用的依赖项来解决此问题。因此,首先,尝试将POM的依赖项更改为springsecurity5。如果这样不起作用,请尝试添加以下@Bean

配置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.extras.springsecurity5.dialect.SpringSecurityDialect;

@Configuration
public class LeafConfig {

    @Bean
    public SpringSecurityDialect springSecurityDialect(){
        return new SpringSecurityDialect();
    }

}

POM

<dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>

由于您使用的是<artifactId>spring-boot-starter-parent</artifactId>,所以请不要在Thymeleaf Extras中添加任何版本,让Spring Boot为您进行管理。