Spring security 4 使用java注解进行登陆验证

本文不深入讲解Spring和Hibernate技术,而是聚焦于使用Spring Security 4进行用户登录验证的实践。通过配置URL权限管理、登录和登出,结合UserDetailsService接口实现从数据库获取用户信息,进行密码验证和权限控制。成功登录后的跳转界面由SuccessHandler处理。了解更多Spring Security内容,建议查阅官方文档。
摘要由CSDN通过智能技术生成

本文只作为spring官方文档的补充,不会解释spring以及hibernate的具体技术细节。
本文默认项目已经具有相关的用户表和DAO,现在我们以此为基础进行基本的用户登陆认证,url权限管理和登出设置。
网上有很多文档描述了如何为项目添加spring security支持。在spring boot中,我们只需要添加了配置类,就可以描述spring security的控制信息:

package edu.nju.web.security;

import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.password.StandardPasswordEncoder;

import javax.annotation.Resource;

/**
 * Http security config
 * @author cuiods
 */
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
   

    @Resource
    private UserDetailsService userService;

    @Resource
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值