问题:原书中创建的User对象在H2 database中为关键词,导致h2 database报错类似【org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "drop table if exists [*]user CASCADE "; expected "identifier"; SQL statement: drop table if exists user CASCADE [42001-214]】
规避办法:不能使用user关键词作为H2 database表名,本人采用tuser作为表名及类名绕过了此bug。
问题:报错【role should not start with 'ROLE_' since it is automatically inserted. Got 'ROLE_USER' at org.springframework.security.config.annotation】
原因及解决办法:原因上面错误已指出,不应该以ROLE_开头,解决办法:
需要将代码
- @Override
- protected void configure(HttpSecurity http) throws Exception {
- http
- .authorizeRequests()
- .antMatchers("/design", "/orders")
- .hasRole("ROLE_USER") //导致bug的代码
- .antMatchers(“/”, "/**").permitAll();
- }
改为如下
- @Override
- protected void configure(HttpSecurity http) throws Exception {
- http
- .authorizeRequests()
- .antMatchers("/design", "/orders")
- .access("hasRole('ROLE_USER')") //修正后的代码
- .antMatchers(“/”, "/**").access("permitAll");
- }
问题:userRepository中的 findByUsername()总是返回空,导致DesignTacoController.java的showDesignForm方法中报错java.lang.NullPointerException: null
解决办法:
1.检查user.java中是否自动生成代码时设置username为空了,为空则去掉
2.对JPA 的指定方法进行自定义SQL查询,代码如下
- public interface TuserRepository extends CrudRepository<Tuser,Long> {
- @Query(value = "SELECT id, username,password FROM TUSER where username=?1",nativeQuery = true)
- Tuser findByUsername(String username);
- }
问题:/design页面提交时总报错
原因及解决办法:design页面中有两个form表单,需要为每个表单添加action标记,以便每个表单提交后知道具体转向哪个url对应的controller。具体如下: