diff --git a/src/main/java/com/codesdream/ase/AseApplication.java b/src/main/java/com/codesdream/ase/AseApplication.java index 9130640..d06b3f4 100644 --- a/src/main/java/com/codesdream/ase/AseApplication.java +++ b/src/main/java/com/codesdream/ase/AseApplication.java @@ -2,6 +2,7 @@ package com.codesdream.ase; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; @SpringBootApplication public class AseApplication { diff --git a/src/main/java/com/codesdream/ase/component/ASEPasswordEncoder.java b/src/main/java/com/codesdream/ase/component/ASEPasswordEncoder.java index 4665e09..62e284f 100644 --- a/src/main/java/com/codesdream/ase/component/ASEPasswordEncoder.java +++ b/src/main/java/com/codesdream/ase/component/ASEPasswordEncoder.java @@ -13,6 +13,6 @@ public class ASEPasswordEncoder implements PasswordEncoder { @Override public boolean matches(CharSequence charSequence, String s) { - return s.equals(charSequence.toString()); + return s.equals(DigestUtils.sha256Hex(charSequence.toString())); } } diff --git a/src/main/java/com/codesdream/ase/component/UserFunctionalPermissionContainersListGenerator.java b/src/main/java/com/codesdream/ase/component/UserFunctionalPermissionContainersListGenerator.java index 01df7e4..49f2c20 100644 --- a/src/main/java/com/codesdream/ase/component/UserFunctionalPermissionContainersListGenerator.java +++ b/src/main/java/com/codesdream/ase/component/UserFunctionalPermissionContainersListGenerator.java @@ -12,8 +12,13 @@ import java.util.Collection; */ @Component public class UserFunctionalPermissionContainersListGenerator { - @Resource - UserFunctionalScopeRelationsListGenerator functionalScopeRelationsListGenerator; + + private UserFunctionalScopeRelationsListGenerator functionalScopeRelationsListGenerator; + + public UserFunctionalPermissionContainersListGenerator(){ + this.functionalScopeRelationsListGenerator = + new UserFunctionalScopeRelationsListGenerator(); + } public Collection generateFunctionalContainers( Collection functionalScopeRelations){ diff --git a/src/main/java/com/codesdream/ase/component/UserFunctionalScopeRelationsListGenerator.java b/src/main/java/com/codesdream/ase/component/UserFunctionalScopeRelationsListGenerator.java index d81a437..5a6f8a8 100644 --- a/src/main/java/com/codesdream/ase/component/UserFunctionalScopeRelationsListGenerator.java +++ b/src/main/java/com/codesdream/ase/component/UserFunctionalScopeRelationsListGenerator.java @@ -14,8 +14,13 @@ import java.util.Collection; */ @Component public class UserFunctionalScopeRelationsListGenerator { - @Resource - UserPermissionContainersCollectionsListGenerator userPermissionContainersCollectionsListGenerator; + + private UserPermissionContainersCollectionsListGenerator userPermissionContainersCollectionsListGenerator; + + public UserFunctionalScopeRelationsListGenerator(){ + this.userPermissionContainersCollectionsListGenerator = + new UserPermissionContainersCollectionsListGenerator(); + } public Collection generateFunctionalScopeRelations( Collection permissionContainersCollections){ diff --git a/src/main/java/com/codesdream/ase/component/UserPermissionContainersCollectionsListGenerator.java b/src/main/java/com/codesdream/ase/component/UserPermissionContainersCollectionsListGenerator.java index 876468b..3baade0 100644 --- a/src/main/java/com/codesdream/ase/component/UserPermissionContainersCollectionsListGenerator.java +++ b/src/main/java/com/codesdream/ase/component/UserPermissionContainersCollectionsListGenerator.java @@ -16,8 +16,13 @@ import java.util.Optional; */ @Component public class UserPermissionContainersCollectionsListGenerator { - @Resource - UserTagsListGenerator userTagsListGenerator; + + private UserTagsListGenerator userTagsListGenerator; + + public UserPermissionContainersCollectionsListGenerator(){ + this.userTagsListGenerator = + new UserTagsListGenerator(); + } public Collection generatePermissionContainerCollections( Collection tags){ diff --git a/src/main/java/com/codesdream/ase/component/UserRolesListGenerator.java b/src/main/java/com/codesdream/ase/component/UserRolesListGenerator.java index e77cba1..5c9f19b 100644 --- a/src/main/java/com/codesdream/ase/component/UserRolesListGenerator.java +++ b/src/main/java/com/codesdream/ase/component/UserRolesListGenerator.java @@ -2,6 +2,7 @@ package com.codesdream.ase.component; import com.codesdream.ase.model.permission.FunctionalPermissionContainer; import com.codesdream.ase.model.permission.User; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.stereotype.Component; @@ -16,8 +17,13 @@ import java.util.Optional; */ @Component public class UserRolesListGenerator { - @Resource - UserFunctionalPermissionContainersListGenerator functionalPermissionContainersListGenerator; + + private UserFunctionalPermissionContainersListGenerator functionalPermissionContainersListGenerator; + + public UserRolesListGenerator(){ + this.functionalPermissionContainersListGenerator = + new UserFunctionalPermissionContainersListGenerator(); + } public Collection generateRoles( Collection functionalPermissionContainers){ @@ -37,6 +43,7 @@ public class UserRolesListGenerator { } public Collection generateRoles(User user){ + return generateRoles( functionalPermissionContainersListGenerator.generateFunctionalPermissionContainers(user) ); diff --git a/src/main/java/com/codesdream/ase/configure/CustomWebSecurityConfig.java b/src/main/java/com/codesdream/ase/configure/CustomWebSecurityConfig.java index 7250446..ef67a73 100644 --- a/src/main/java/com/codesdream/ase/configure/CustomWebSecurityConfig.java +++ b/src/main/java/com/codesdream/ase/configure/CustomWebSecurityConfig.java @@ -20,10 +20,10 @@ import javax.annotation.Resource; @EnableWebSecurity public class CustomWebSecurityConfig extends WebSecurityConfigurerAdapter { - @Autowired + @Resource ASEUserDetailsService aseUserDetailService; - @Autowired + @Resource ASEPasswordEncoder asePasswordEncoder; @Override @@ -33,9 +33,8 @@ public class CustomWebSecurityConfig extends WebSecurityConfigurerAdapter { .and() .csrf().disable().formLogin() .and() - .formLogin().loginPage("/login").permitAll().defaultSuccessUrl("/").permitAll() - .usernameParameter("username") - .passwordParameter("password") + .formLogin().loginPage("/login") + .permitAll().defaultSuccessUrl("/").permitAll() .and() .logout().permitAll(); @@ -43,13 +42,14 @@ public class CustomWebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { - auth.inMemoryAuthentication().withUser("user").password("password").roles("普通用户"); + auth.userDetailsService(aseUserDetailService) + .passwordEncoder(asePasswordEncoder); } @Override public void configure(WebSecurity web) throws Exception { web .ignoring() - .antMatchers("/assets/**"); + .antMatchers("/assets/**", "/register/**", "/forget/**", "/not_found/**"); } } diff --git a/src/main/java/com/codesdream/ase/controller/RegisterController.java b/src/main/java/com/codesdream/ase/controller/RegisterController.java index 259cf93..c8a3aca 100644 --- a/src/main/java/com/codesdream/ase/controller/RegisterController.java +++ b/src/main/java/com/codesdream/ase/controller/RegisterController.java @@ -1,4 +1,61 @@ package com.codesdream.ase.controller; +import com.codesdream.ase.component.ASEPasswordEncoder; +import com.codesdream.ase.model.permission.User; +import com.codesdream.ase.repository.UserRepository; +import com.codesdream.ase.service.UserService; +import org.springframework.boot.autoconfigure.security.SecurityProperties; +import org.springframework.http.HttpRequest; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.HttpRequestHandler; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import java.util.Map; +import java.util.Objects; + +@Controller public class RegisterController { + @Resource + UserService userService; + + @RequestMapping(value = "/register") + String registerView(Model model){ + return "register"; + } + + // 处理注册表单 + @PostMapping(value = "/register") + String doRegister(Model model, HttpServletRequest request){ + Map parameterMap= request.getParameterMap(); + + // 进行处理前的检查 + if(parameterMap.containsKey("username") + && parameterMap.containsKey("password") + && parameterMap.containsKey("retry-password") + && parameterMap.containsKey("user-question") + && parameterMap.containsKey("user-answer") + ) { + User user = userService.getDefaultUser(); + user.setUsername(parameterMap.get("username")[0].toString()); + + String password = parameterMap.get("password")[0].toString(); + String retry_password = parameterMap.get("retry-password")[0].toString(); + + if (password.equals(retry_password)) { + user.setPassword(password); + userService.save(user); + + return "index"; + } + + } + + return "register"; + } + } diff --git a/src/main/java/com/codesdream/ase/model/permission/User.java b/src/main/java/com/codesdream/ase/model/permission/User.java index 4f7f936..45c446a 100644 --- a/src/main/java/com/codesdream/ase/model/permission/User.java +++ b/src/main/java/com/codesdream/ase/model/permission/User.java @@ -25,10 +25,6 @@ public class User implements UserDetails { @Column(nullable = false) private String password; - // 手机号 - @Column(nullable = true) - private String phone_number; - // 账号是否过期 private boolean accountNonExpired; @@ -57,7 +53,7 @@ public class User implements UserDetails { this.username = username; this.password = password; - initUserDefault(); + initDefault(); } public User() { @@ -65,16 +61,18 @@ public class User implements UserDetails { this.password = null; this.deleted = false; - initUserDefault(); + initDefault(); } // 用默认的方式初始化User对象的值 - private void initUserDefault(){ + private void initDefault(){ this.accountNonExpired = true; this.accountNonLocked = true; this.credentialsNonExpired = true; this.deleted = false; + this.enabled = true; this.tags = new HashSet<>(); + this.userDetail = new UserDetail(); } @Override diff --git a/src/main/java/com/codesdream/ase/service/ASEUserDetailsService.java b/src/main/java/com/codesdream/ase/service/ASEUserDetailsService.java index 3808771..8c0d9b8 100644 --- a/src/main/java/com/codesdream/ase/service/ASEUserDetailsService.java +++ b/src/main/java/com/codesdream/ase/service/ASEUserDetailsService.java @@ -1,5 +1,6 @@ package com.codesdream.ase.service; +import com.codesdream.ase.model.permission.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; @@ -7,6 +8,7 @@ import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.transaction.Transactional; +import java.util.Optional; @Service public class ASEUserDetailsService implements UserDetailsService { @@ -17,11 +19,15 @@ public class ASEUserDetailsService implements UserDetailsService { @Override @Transactional public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException { - if(!userService.findUserByUsername(s).isPresent()){ - throw new UsernameNotFoundException("用户不存在"); + System.out.println("User: " + s); + Optional user = userService.findUserByUsername(s); + if(!user.isPresent()){ + throw new UsernameNotFoundException("User Not Found"); } else { - return userService.findUserByUsername(s).get(); + System.out.println("Returning user information"); + System.out.println("User Password: "+user.get().getPassword()); + return user.get(); } } } diff --git a/src/main/java/com/codesdream/ase/service/IPermissionService.java b/src/main/java/com/codesdream/ase/service/IPermissionService.java new file mode 100644 index 0000000..f4d7108 --- /dev/null +++ b/src/main/java/com/codesdream/ase/service/IPermissionService.java @@ -0,0 +1,4 @@ +package com.codesdream.ase.service; + +public interface IPermissionService { +} diff --git a/src/main/java/com/codesdream/ase/service/IUserService.java b/src/main/java/com/codesdream/ase/service/IUserService.java index 32919fd..2c68e50 100644 --- a/src/main/java/com/codesdream/ase/service/IUserService.java +++ b/src/main/java/com/codesdream/ase/service/IUserService.java @@ -12,4 +12,7 @@ public interface IUserService { Optional findUserById(int id); Optional findUserByUsername(String username); User save(User user); + // 获得默认用户 + User getDefaultUser(); + } diff --git a/src/main/java/com/codesdream/ase/service/PermissionService.java b/src/main/java/com/codesdream/ase/service/PermissionService.java new file mode 100644 index 0000000..8147826 --- /dev/null +++ b/src/main/java/com/codesdream/ase/service/PermissionService.java @@ -0,0 +1,8 @@ +package com.codesdream.ase.service; + +import org.springframework.stereotype.Service; + +@Service +public class PermissionService implements IPermissionService { + +} diff --git a/src/main/java/com/codesdream/ase/service/UserService.java b/src/main/java/com/codesdream/ase/service/UserService.java index 8b62559..8ff8f86 100644 --- a/src/main/java/com/codesdream/ase/service/UserService.java +++ b/src/main/java/com/codesdream/ase/service/UserService.java @@ -34,11 +34,18 @@ public class UserService implements IUserService { @Override public User save(User user) { + if(userRepository.findByUsername(user.getUsername()).isPresent()) + throw new RuntimeException("Username Already Exists"); user.setPassword(asePasswordEncoder.encode(user.getPassword())); user.setEnabled(true); user.setDeleted(false); return userRepository.save(user); } + @Override + public User getDefaultUser() { + return new User(); + } + } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index bc148d1..e33dd03 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -9,6 +9,8 @@ spring.jpa.generate-ddl=false spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update spring.jooq.sql-dialect=org.hibernate.dialect.MySQL5InnoDBDialect +spring.jpa.open-in-view=true +spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true spring.datasource.url=jdbc:mysql://${MYSQL_HOST:119.23.9.34}:3306/ase?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.username=codedream diff --git a/src/main/resources/templates/error.html b/src/main/resources/templates/error.html new file mode 100644 index 0000000..672d490 --- /dev/null +++ b/src/main/resources/templates/error.html @@ -0,0 +1,23 @@ + + + + + + + + + +
+ +error + + + +
+

Error

+
+ + + + + \ No newline at end of file diff --git a/src/main/resources/templates/forget.html b/src/main/resources/templates/forget.html new file mode 100644 index 0000000..e2e3398 --- /dev/null +++ b/src/main/resources/templates/forget.html @@ -0,0 +1,56 @@ + + + + + + + + + + +
+ + +
+
+
+
+
+
+
+

重置密码

+
+
+
+ +
+ + + 获取密保问题 + + + +
+
+ + +
+
+ + +
+
+
+ +
+
+
+
+
+
+
+
+
+ + diff --git a/src/main/resources/templates/login.html b/src/main/resources/templates/login.html index 685432c..9c21e7f 100644 --- a/src/main/resources/templates/login.html +++ b/src/main/resources/templates/login.html @@ -2,14 +2,13 @@ -
+
-
-
+
@@ -19,20 +18,20 @@
- +
- - +
- - + 忘记密码 + 注册账号 +
diff --git a/src/main/resources/templates/not_found.html b/src/main/resources/templates/not_found.html new file mode 100644 index 0000000..f892a34 --- /dev/null +++ b/src/main/resources/templates/not_found.html @@ -0,0 +1,23 @@ + + + + + + + + + +
+ +notfound + + + +
+

404 Not Found

+
+ + + + + diff --git a/src/main/resources/templates/register.html b/src/main/resources/templates/register.html new file mode 100644 index 0000000..f9dc699 --- /dev/null +++ b/src/main/resources/templates/register.html @@ -0,0 +1,51 @@ + + + +
+ + +
+
+
+
+
+
+
+

注册

+
+
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ + +
+
+
+ +
+
+
+
+
+
+
+
+
+ +