Spring?Security是基于Spring的一個(gè)安全管理框架。它相比Shiro,它的功能更豐富,社區(qū)資源也比Shiro更豐富!
一:Spring Security授權(quán)認(rèn)證登錄實(shí)現(xiàn)
1.導(dǎo)入Maven依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.7.5</version>
</dependency>
2.創(chuàng)建實(shí)體類User
package com.yq.pojo;
import com.baomidou.mybatisplus.annotation.TableName;
@TableName("sys_user")
public class SysUser {
private long userId;
private long deptId;
private String username;
private String nickName;
private String gender;
private String phone;
private String email;
private String avatarName;
private String avatarPath;
private String password;
private String isAdmin;
private long enabled;
private String createBy;
private String updateBy;
private java.sql.Timestamp pwdResetTime;
private java.sql.Timestamp createTime;
private java.sql.Timestamp updateTime;
}
3.訪問(wèn)數(shù)據(jù)庫(kù)并驗(yàn)證用戶密碼
@Mapper
public interface userMapper extends BaseMapper<SysUser> {
}
為了偷懶,我這里用的是Mybatis plus,哈哈哈哈
4.添加Spring Security的配置
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
@Configuration
public class SecurityConfig1 extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(getPasswordEncoder());
}
//密碼加密的類
@Bean
PasswordEncoder getPasswordEncoder(){
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.exceptionHandling().accessDeniedPage("/logOut.html");//無(wú)權(quán)限時(shí)跳轉(zhuǎn)的頁(yè)面
http.logout().logoutUrl("/gg").logoutSuccessUrl("static/index.html").permitAll();//注銷登錄
http.formLogin()
.loginPage("/index.html")//登錄頁(yè)面設(shè)置
.loginProcessingUrl("/user/login")//指定登錄頁(yè)面要跳轉(zhuǎn)到的頁(yè)面路徑
.defaultSuccessUrl("/work/info/1/5")//默認(rèn)的成功跳轉(zhuǎn)頁(yè)面路徑
.and().authorizeRequests()
.antMatchers("/","/index.html").permitAll()
// .antMatchers("/").hasAnyAuthority("sb")//擁有這個(gè)權(quán)限才能訪問(wèn)該頁(yè)面
.anyRequest().authenticated()
.and().csrf().disable();//關(guān)閉csrf防護(hù)
}
}
繼承WebSecurityConfigurerAdapter 類并實(shí)現(xiàn)configure兩個(gè)方法(同名),注入PasswordEncoder 類(密碼加密類)
該類是對(duì)用戶權(quán)限的配置文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-550779.html
5.編寫Service層實(shí)現(xiàn)UserDetailsService?
@Service("userDetailsService")
public class MyDetailsPassWord implements UserDetailsService {
@Autowired
private userMapper userMapper;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
QueryWrapper<SysUser> qw = new QueryWrapper<>();
qw.eq("username", username);
SysUser user = userMapper.selectOne(qw);
if (user == null) {
throw new UsernameNotFoundException("用戶不存在呀!!貼子");
}
//賦予權(quán)限
System.out.println("授予權(quán)限!!"+username+"密碼:"+user.getPassword());
List<GrantedAuthority> auths = AuthorityUtils.commaSeparatedStringToAuthorityList("hyq,admin,sb");
return new User(user.getUsername(), new BCryptPasswordEncoder().encode(user.getPassword()), auths);
}
}
實(shí)現(xiàn)loadUserByUsername()方法對(duì)用戶進(jìn)行驗(yàn)證是否存在并授權(quán)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-550779.html
到了這里,關(guān)于Spring Security安全攔截基礎(chǔ)實(shí)現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!