SEC:授权不在Spring-Boot应用程序中工作

发布于 2025-02-11 04:52:39 字数 3247 浏览 0 评论 0原文

我在资源/模板上有此index.html我项目的目录:

<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" th:href="@{/css/style.css}">
</head>
<body>
    <nav sec:authorize="isAnonymous()">
        <strong>Hello World!</strong> <a th:href="@{/login}">Login</a>
    </nav>

    <nav sec:authorize="isAuthenticated()">
        <strong th:text="${usuario}"></strong> <a th:href="@{/logout}">Logout</a>
    </nav>

    <script th:src="@{/js/script.js}"></script>
</body>
</html>

sec中的标签都不或内部sec:授权=“ Isauthenticated()”在我运行项目并在浏览器中打开时正在显示

我有此application.properties文件:

security.basic.enabled=false

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/appdata
spring.datasource.username=kleber
spring.datasource.password=123456
spring.datasource.continue-on-error=true

sprinf.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
spring.servlet.multipart.file-size-threshold=10MB

server.tomcat.max-http-post-size=10MB

和此应用程序类:

@SpringBootApplication
@EnableWebSecurity
@Controller
public class AppApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(AppApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(AppApplication.class);
    }

    @Bean
    public WebSecurityCustomizer ignoringCustomizer() {
           return (web) -> web.ignoring().antMatchers("/", "/login", "/logout", "/error", "/css/**", "/js/**", "/img/**");
    }

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

    public UserDetailsService userDetailsService() {
        return new UserDetailsService() {
            @Autowired
            private UsuarioDao usuarioDao;

            @Override
            public org.springframework.security.core.userdetails.UserDetails loadUserByUsername(String username) {
                return usuarioDao.findBy("username", username).get(0);
            }
        };
    }

    @RequestMapping(value = "/")
    public String index(Model model) {
        return "index";
    }

    @RequestMapping(value = "/login")
    public String login(Model model) {
        return "login";
    }

}

这里有什么问题的暗示?

I have this index.html on the resources/templates directory of my project:

<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" th:href="@{/css/style.css}">
</head>
<body>
    <nav sec:authorize="isAnonymous()">
        <strong>Hello World!</strong> <a th:href="@{/login}">Login</a>
    </nav>

    <nav sec:authorize="isAuthenticated()">
        <strong th:text="${usuario}"></strong> <a th:href="@{/logout}">Logout</a>
    </nav>

    <script th:src="@{/js/script.js}"></script>
</body>
</html>

Neither the tags inside sec:authorize="isAnonymous()" or inside sec:authorize="isAuthenticated()" are being display when I run the project and open it in the browser.

I have this application.properties file:

security.basic.enabled=false

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/appdata
spring.datasource.username=kleber
spring.datasource.password=123456
spring.datasource.continue-on-error=true

sprinf.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
spring.servlet.multipart.file-size-threshold=10MB

server.tomcat.max-http-post-size=10MB

and this App class:

@SpringBootApplication
@EnableWebSecurity
@Controller
public class AppApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(AppApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(AppApplication.class);
    }

    @Bean
    public WebSecurityCustomizer ignoringCustomizer() {
           return (web) -> web.ignoring().antMatchers("/", "/login", "/logout", "/error", "/css/**", "/js/**", "/img/**");
    }

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

    public UserDetailsService userDetailsService() {
        return new UserDetailsService() {
            @Autowired
            private UsuarioDao usuarioDao;

            @Override
            public org.springframework.security.core.userdetails.UserDetails loadUserByUsername(String username) {
                return usuarioDao.findBy("username", username).get(0);
            }
        };
    }

    @RequestMapping(value = "/")
    public String index(Model model) {
        return "index";
    }

    @RequestMapping(value = "/login")
    public String login(Model model) {
        return "login";
    }

}

Any hints of what's wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北斗星光 2025-02-18 04:52:39

您是否包括弹簧安全性百里香扩展名?

如果您使用的是Maven,则依赖性应该是这样的:

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

您还需要在HTML内启用胸腺ta虫标签:

<html xmlns:sec="http://www.thymeleaf.org/extras/spring-security">

Heres对此进行了更多信息: https://www.thymeleaf.org/doc/doc/articles/springsecurity.html

我也刚刚意识到这是一个重复: /stackoverflow.com/questions/56187017/thymeleaf-spring-security-integration-integration-secauthorize-is-not-working"> themeleaf Spring Security集成SEC:授权不起作用

Did you include Spring Security Thymeleaf extension?

If you're using Maven, the dependency should look like this:

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

Also you will need to enable the Thymeleaf Tags inside the HTML:

<html xmlns:sec="http://www.thymeleaf.org/extras/spring-security">

Heres more on that: https://www.thymeleaf.org/doc/articles/springsecurity.html

I also just realized this is a duplicate: Thymeleaf Spring security integration sec:authorize is not working

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文