spring security +h2+springboot+jpa加载先后问题.

spring security +h2+springboot+jpa加载先后问题.

问题情况:

我需要创建一个自定义数据库,为了方便就使用了h2,并利用classpath下的data.sql插入数据.

其中利用User和Role实体映射成mooc_User和mooc_Role表.

并让spring security读取其中的user和role表,当做users和authorities;

但结果是:

Table "mooc_users" not found (this database is empty); SQL statement:,

更具体的控制台输出:

image-20220125222728045

经过多方面的debug以及某些包的日志输出设置为(debug),终于确定属于bean加载先后问题.

本来应该先将实体类映射成为表,然后插入数据,但没想到表还没映射,就开始读取data.sql中的数据了.

好稀奇的一件事,之前竟然从来没遇到过.

在网上找了各种各样的资料,

如下:

怎么控制springboot中bean的加载顺序 - 开发技术 - 亿速云 (yisu.com)

[使用@AutoConfigureBefore调整配置顺序竟没生效? - YourBatman - 博客园 (cnblogs.com)](https://www.cnblogs.com/yourbatman/p/13264743.html#:~:text=Spring Boot下控制配置执行顺序 Spring Boot 下对 自动配置,的管理对比于Spring它就是黑盒,它会根据当前容器内的情况来 动态的 判断自动配置类的加载与否、以及加载的顺序,所以可以说:Spring Boot的自动配置它对顺序是有 强要求 的。)

(27条消息) SpringBoot控制配置类加载顺序_zengjyxxz的博客-CSDN博客_springboot配置类加载顺序

虽然大致知道了应该怎么设置加载顺序,但是因为项目有点大,我实在不太敢改动,因为改动的地方不止一处,所谓牵一发而动全身,我怕错误越改越多,就没敢再动.

就只能修改一些比较浅薄的内容.

但是在控制台中debug出的内容,很多配置都不一样,最后就改了springboot的版本.

本来是2.6.3,最后改成了

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

成功!

这种法子有点莫名其妙,但好在还勉强能成功.但是上述的三个链接我会创建一个项目多个模块一点点试,好歹多学了一些知识,也不算亏.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现登录模块需要前后端配合完成,以下是一个简单的示例: 1. 后端使用Spring Boot实现: 首先,需要引入Spring Security依赖,可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 接着,在Spring Boot的配置类中添加以下配置: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/login").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .defaultSuccessUrl("/home") .permitAll() .and() .logout() .logoutSuccessUrl("/") .permitAll(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } } ``` 其中,`configure()`方法配置了哪些请求需要进行验证,以及登录、登出的相关配置。`configureGlobal()`方法配置了用户信息的获取方式,这里使用了一个自定义的`UserDetailsService`接口来获取用户信息。`passwordEncoder()`方法配置了密码加密方式,这里使用了BCrypt加密算法。 接下来,实现`UserDetailsService`接口以及相关实体类: ```java @Service public class UserDetailsServiceImpl implements UserDetailsService { @Autowired private UserRepository userRepository; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userRepository.findByUsername(username); if (user == null) { throw new UsernameNotFoundException("User not found"); } return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), emptyList()); } } @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false, unique = true) private String username; @Column(nullable = false) private String password; // 省略getter、setter、构造方法等 } public interface UserRepository extends JpaRepository<User, Long> { User findByUsername(String username); } ``` 这里使用了JPA来操作数据库,`UserDetailsService`接口使用了自定义的实现类`UserDetailsServiceImpl`,通过`UserRepository`来获取用户信息。 2. 前端使用Vue实现: 首先,在Vue项目中安装axios和vue-router依赖: ``` npm install axios vue-router --save ``` 接着,可以在main.js中添加以下代码来配置axios: ```javascript import axios from 'axios' Vue.prototype.$http = axios ``` 然后,在Vue项目中创建一个登录页面Login.vue,实现登录功能: ```html <template> <div> <h2>Login</h2> <form @submit.prevent="login"> <div> <label>Username:</label> <input type="text" v-model="username"> </div> <div> <label>Password:</label> <input type="password" v-model="password"> </div> <button type="submit">Login</button> </form> </div> </template> <script> export default { data() { return { username: '', password: '' } }, methods: { login() { this.$http.post('/login', { username: this.username, password: this.password }).then(response => { // 登录成功后跳转到主页 this.$router.push('/home') }).catch(error => { console.log(error) }) } } } </script> ``` 在上面的代码中,使用了axios来发送POST请求,将用户名和密码发送到后端进行验证,如果验证成功则跳转到主页。 以上是一个简单的示例,实际项目中还需要进行一些安全性、效率性的优化,例如加入验证码、防止暴力破解等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值