SEC:授权不在Spring-Boot应用程序中工作
我在资源/模板上有此
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否包括弹簧安全性百里香扩展名?
如果您使用的是Maven,则依赖性应该是这样的:
您还需要在HTML内启用胸腺ta虫标签:
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:
Also you will need to enable the Thymeleaf Tags inside the HTML:
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