springboot学习之权限系统登录验证SpringSecurity

SpringSecurity核心功能:认证、授权、攻击防护(防止伪造身份)

涉及的依赖如下:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

新建一个项目,添加如上依赖,在控制器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

 若之前访问的页面是控制器中配置的页面http://localhost:8080/hello

则此时能成功显示

若之前范文的页面是其他的,控制器中未配置的,则重定向后返回页面不存在。

当前自己的项目中,总不能用系统生成的密码进行登录获得权限,那不要被别人笑死。

进阶阶段:

我简单创建了一张表,希望该表的人输入匹配的用户名和密码后,方能登录。

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<User> {
    @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的方法;

还有判断账户是否过期,被锁,验证是否过期,是否开启了。。。

 

猜你喜欢

转载自www.cnblogs.com/qianjinyan/p/10363504.html