springboot 按钮权限验证_springboot学习之权限系统登录验证SpringSecurity
SpringSecurity核心功能:認證、授權、攻擊防護(防止偽造身份)
涉及的依賴如下:
org.springframework.boot
spring-boot-starter-security
新建一個項目,添加如上依賴,在控制器controller中測試,指定url,比如
@Controller
public class UserController {
@RequestMapping(value="/hello")
@ResponseBody
public String hello(){return "=======Welcome to HelloWorld==============";}
}
如上,原本啟動項目后,在地址欄中輸入http://localhost:8080/hello應該顯示返回的內容
然而此次加了安全驗證后,不管url中訪問的地址是什么,hello還是hello111,均返回login頁面,如下
此時系統都沒有連DB,用戶名和密碼是什么?
控制臺中有消息,比如Using generated security password: 76dade1c-f190-44f8-915c-7a6b6917fb9a【每次隨機生成的密碼】
將用戶名 user 和 密碼?76dade1c-f190-44f8-915c-7a6b6917fb9a 填入上面對話框中,點擊按鈕Sign in
則此時能成功顯示
若之前范文的頁面是其他的,控制器中未配置的,則重定向后返回頁面不存在。
當前自己的項目中,總不能用系統生成的密碼進行登錄獲得權限,那不要被別人笑死。
進階階段:
我簡單創建了一張表,希望該表的人輸入匹配的用戶名和密碼后,方能登錄。
CREATE TABLE `admin_user`(
`id` int(4) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(100),
`password` VARCHAR(100),
`role` VARCHAR(100),
`realname` VARCHAR(100),
`mobile` VARCHAR(2000),
`state` BIT default 0,
`info` VARCHAR(200),
PRIMARY KEY (`id`)
)ENGINE=InnoDB AUTO_INCREMENT=300;
塞了幾條數據進去,然后我希望用戶在頁面上進行登錄,那我必須還要創建一個User對象,所謂登錄就是傳入username和password匹配的場景,只要匹配,就登錄成功,跳轉到之前的url
public class User {
private int id;
private String name;
private String password; 省略 getter and setter}
public interface UserService {
User login(String name, String password);
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public User login(String name, String password) {
String sql ="select * from admin_user where username =? and password = ?";
User user =jdbcTemplate.queryForObject(sql,new UserRowMapper(),name,password);
return user;
}
}
public class UserRowMapper implements RowMapper {
@Override
public User mapRow(ResultSet resultSet, int i) throws SQLException {
//此處要使用表中的字段,不能使用屬性
int id =resultSet.getInt("id");
String username = resultSet.getString("username");
String password = resultSet.getString("password");
//String role = resultSet.getString("role");
User user = new User();
user.setId(id);
user.setName(username);
user.setPassword(password);
return user;
}
}
登錄的方法啪啪啪很快就寫好了,我要怎么讓系統知道,所有的請求,要先進行登錄呢,登錄的URL是什么?
先看看別人的代碼,貌似是實現了UserDetailsService 接口,而點進去發現該接口就一個方法
package org.springframework.security.core.userdetails;
public interface UserDetailsService {
UserDetails loadUserByUsername(String var1) throws UsernameNotFoundException;
}
通過一個String類型的變量val1,獲取用戶的詳細信息。。。怎么跟我想的不太一樣?
再點進去發現UserDetails?也是一個接口
package org.springframework.security.core.userdetails;
import java.io.Serializable;
import java.util.Collection;
import org.springframework.security.core.GrantedAuthority;
public interface UserDetails extends Serializable {
Collection extends GrantedAuthority> getAuthorities();
String getPassword();
String getUsername();
boolean isAccountNonExpired();
boolean isAccountNonLocked();
boolean isCredentialsNonExpired();
boolean isEnabled();
}
一個集合,收集權限,結合做過的項目,有的權限是超級管理員,有的權限是普通管理員,又或者有的刪,有新增,有更新等等權限;兩個返回String的方法;
還有判斷賬戶是否過期,被鎖,驗證是否過期,是否開啟了。。。
總結
以上是生活随笔為你收集整理的springboot 按钮权限验证_springboot学习之权限系统登录验证SpringSecurity的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql sql诊断建议_MySQL诊
- 下一篇: gdb加载python_gdb加载pyt