记录一次springboot找不到html问题

记录一次springboot找不到html问题

网上乱起保证都有,但是都没用。
第一,说没引包。

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
 </dependency>

第二,说没有配置好

  thymeleaf:
    prefix: classpath:/templates/ #prefix:指定模板所在的目录
    check-template-location: true  #check-tempate-location: 检查模板路径是否存在
    cache: false  #cache: 是否缓存,开发模式下设置为false,避免改了模板还要重启服务器,线上设置为true,可以提高性能。
    suffix: .html
    encoding: UTF-8
    content-type: text/html
    mode: HTML5

但是我这个都不是,而是编译器的打包的问题,我看到编译后没有html,这时候在pom里面resource添加html就好。

           <directory>src/main/resources</directory>
           <includes>
               <include>*.yml</include>
               <include>**/*.xml</include>
               <include>**/*.html</include>
           </includes>
           <filtering>true</filtering>
       </resource>

一个问题,有千万种可能,需要我们耐心寻找。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 引入依赖 在pom.xml文件中引入Spring Security依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 2. 添加配置类 在项目中添加一个配置类SecurityConfig,继承自WebSecurityConfigurerAdapter。 ``` @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/login", "/register").permitAll() // 登录注册页面不需要验证 .anyRequest().authenticated() // 其他页面需要验证 .and() .formLogin() .loginPage("/login") // 登录页面 .defaultSuccessUrl("/index") // 登录成功后的默认跳转页面 .and() .logout() .logoutUrl("/logout") // 退出登录的URL .logoutSuccessUrl("/login") // 退出登录后跳转到的页面 .invalidateHttpSession(true) .deleteCookies("JSESSIONID"); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() // 在内存中存储用户信息 .withUser("admin").password("{noop}admin").roles("ADMIN") .and() .withUser("user").password("{noop}user").roles("USER"); } } ``` 3. 实现登录与注册功能 创建登录页面login.html和注册页面register.html,使用Thymeleaf模板引擎渲染页面。 在Controller中添加登录和注册的请求处理方法。 ``` @Controller public class UserController { @GetMapping("/login") public String login() { return "login"; } @PostMapping("/login") public String loginSuccess() { return "redirect:/index"; } @GetMapping("/register") public String register() { return "register"; } @PostMapping("/register") public String registerSuccess() { return "redirect:/login"; } } ``` 4. 实现用户鉴权功能 在SecurityConfig中重写configure(AuthenticationManagerBuilder auth)方法,实现用户信息的认证。 这里使用inMemoryAuthentication()方法在内存中存储用户信息,实际应用中可以使用数据库存储。 ``` @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() // 在内存中存储用户信息 .withUser("admin").password("{noop}admin").roles("ADMIN") .and() .withUser("user").password("{noop}user").roles("USER"); } ``` 在Controller中添加需要鉴权的请求处理方法,并在方法上添加@PreAuthorize注解指定需要的权限。 ``` @Controller public class UserController { @GetMapping("/admin") @PreAuthorize("hasRole('ADMIN')") public String admin() { return "admin"; } @GetMapping("/user") @PreAuthorize("hasRole('USER')") public String user() { return "user"; } } ``` 5. 配置登录认证 在SecurityConfig中重写configure(HttpSecurity http)方法,配置登录认证信息。 ``` @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/login", "/register").permitAll() // 登录注册页面不需要验证 .anyRequest().authenticated() // 其他页面需要验证 .and() .formLogin() .loginPage("/login") // 登录页面 .defaultSuccessUrl("/index") // 登录成功后的默认跳转页面 .and() .logout() .logoutUrl("/logout") // 退出登录的URL .logoutSuccessUrl("/login") // 退出登录后跳转到的页面 .invalidateHttpSession(true) .deleteCookies("JSESSIONID"); } ``` 6. 测试 启动应用,在浏览器中访问http://localhost:8080/login,输入用户名和密码进行登录,登录成功后跳转到首页。 访问http://localhost:8080/admin和http://localhost:8080/user,根据用户角色的不同,页面会有不同的显示。如果访问没有权限的页面,会自动跳转到登录页面。在登录状态下访问/logout可以退出登录。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值