實作效果:
實作使用者登陸功能,與傳統的登陸功能不同,用
Spring-Security
實作登陸,減少了開發者的代碼,也提高了系統的安全性

技術棧:
- Lombok(@Data自動生成get和set方法)
- mybatis-plus(生成了多數簡單的sql)
- mybatis-plus代碼生成器(生成了基礎的三層)
- Spring-Security安全架構(主要實作)
目錄結構
實作(持久層)
- 根據使用者名查詢使用者資訊
@Select("select * from user where username=#{username}")
User finUserByUsername(String username);
- 根據使用者id查詢使用者權限
@Select("select p.id,p.name,p.desc " +
"from user u " +
"left join user_role ur on u.id=ur.user_id " +
"left join role r on r.id=ur.role_id " +
"left join role_permission rp on r.id=rp.role_id " +
"left join permission p on p.id=rp.permission_id " +
"where u.id=#{id}")
List<Permission>findUserPermissionsById(Integer id);
實作(業務層)
1.業務層接口
IUserService
public interface IUserService extends IService<User> {
/**
* 根據username擷取使用者權限,username->UserDetails
* @param username
* @return
*/
UserDetails getUserDetails(String username);
}
2.業務層實作類
UserServiceImpl
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
@Autowired
private UserMapper userMapper;
/**
* 根據username擷取使用者權限,username->UserDetails
* 涉及持久層操作(持久層)
* 1:根據使用者名username查詢User表是否存在
* 2:傳回給Security安全架構
* @param username
* @return
*/
@Override
public UserDetails getUserDetails(String username) {
User user=userMapper.finUserByUsername(username);
if(user==null){
return null;
}
List<Permission> permissions = userMapper.findUserPermissionsById(user.getId());
String [] authorities=new String[permissions.size()];
int i=0;
for (Permission p : permissions) {
authorities[i++]=p.getName();
}
UserDetails u= org.springframework.security.core.userdetails.User
.builder()
.username(user.getUsername())
.password(user.getPassword())
.accountLocked(user.getLocked()==1)
.disabled(user.getEnabled()==0)
.authorities(authorities)
.build();
return u;
}
}
業務層實作類實作效果:根據
username
使用者名傳回一個
UserDetails
對象交給
UserDetailsServiceIml
實作類
3.UserDetailsServiceIml實作類
@Component
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private IUserService userService;
/**
* 通過UserService業務層驗證傳回UserDetails對象,作為權限驗證資訊
* @param username
* @return
* @throws UsernameNotFoundException
*/
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return userService.getUserDetails(username);
}
}
4.
SercurityConfig
Spring-security配置類
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
UserDetailsServiceImpl userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().
authorizeRequests().antMatchers(
"/css/*",
"/img/*",
"/js/*",
"/browser_components/**",
"/login.html",
"/register.html",
"/register"
).permitAll()
.anyRequest().authenticated()
.and().formLogin()
.loginPage("/login.html")
.loginProcessingUrl("/login")
.failureUrl("/login.html?error")
.defaultSuccessUrl("/index.html")
.and().logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/login.html?logout");
}
}
實作(控制器)
@RestController
@Slf4j
public class SystemController {
@GetMapping("/login.html")
public ModelAndView loginForm(){
return new ModelAndView("login");
}
}
此時就已經實作了登陸,幾乎都是配置資訊,交給Spring-Security管理,開發者非常友善,系統又非常安全