项目目录结构如下:

首先数据库的建立和数据导入,以及一些类的依赖参考XML配置方式,需要修改一些配置。

一、在AppConfig文件中添加DataSource的配置
@Bean(name = "dataSource")
public DriverManagerDataSource dataSource() {
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
driverManagerDataSource.setUrl("jdbc:mysql://192.168.11.81:3306/security_learning");
driverManagerDataSource.setUsername("petter");
driverManagerDataSource.setPassword("petter");
return driverManagerDataSource;
}

类似于spring-database.xml的配置

修改:
@Bean
public SpringTemplateEngine springTemplateEngine() {
SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
springTemplateEngine.setTemplateResolver(springResourceTemplateResolver());
springTemplateEngine.addDialect(new SpringSecurityDialect());
return springTemplateEngine;
}

添加SpringSecurityDialect

二、修改SecurityConfig文件
package com.petter.config;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import javax.annotation.Resource;
import javax.sql.DataSource;
/**
* 相当于spring-security.xml中的配置
* @author hongxf
* @since 2017-03-08 9:30
*/
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Resource
private DataSource dataSource;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery("select username,password, enabled from users where username = ?")
.authoritiesByUsernameQuery("select username, role from user_roles where username = ?");
}
/**
* 配置权限要求
* 采用注解方式,默认开启csrf
* @param http
* @throws Exception
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/dba/**").hasAnyRole("ADMIN", "DBA")
.and()
.formLogin().loginPage("/login")
.defaultSuccessUrl("/welcome").failureUrl("/login?error")
.usernameParameter("user-name").passwordParameter("pwd")
.and()
.logout().logoutSuccessUrl("/login?logout")
.and()
.exceptionHandling().accessDeniedPage("/403")
.and()
.csrf();
}
}

三、html文件的添加和修改参考XML配置方式

启动程序测试即可
 

spring security结合数据库验证用户-注解方式的更多相关文章

  1. spring security结合数据库验证用户-XML配置方式

    之前的用户信息我们都是使用的内存用户,测试例子可以,实际中使用肯定不行,需要结合数据库进行验证用户.这就是本节的重点: 项目目录如下:  在之前的项目中的依赖中添加两个依赖: <dependen ...

  2. spring security使用数据库验证的逻辑处理

    前面做了多个示例,包括使用jdbc和hibernate两种方式访问数据库获取用户信息和权限信息,其中一些关键步骤如下:   我们在SecurityConfig中配置覆盖configure方法时候,可以 ...

  3. spring security关闭http验证 和 springboot 使用h2数据库

    spring security关闭http验证 最近在跑demo的过程中,访问swagger页面的时候需要验证登录,记得在之前写的代码中是关闭了security验证,无需登录成功访问,直接在appli ...

  4. Spring Security 概念基础 验证流程

    Spring Security 概念基础 验证流程 认证&授权 认证:确定是否为合法用户 授权:分配角色权限(分配角色,分配资源) 认证管理器(Authentication Manager) ...

  5. spring security+freemarker获取登陆用户的信息

    spring security+freemarker获取登陆用户的信息 目标页面之间获取 ${Session.SPRING_SECURITY_CONTEXT.authentication.princi ...

  6. 自定义Spring Security的身份验证失败处理

    1.概述 在本快速教程中,我们将演示如何在Spring Boot应用程序中自定义Spring Security的身份验证失败处理.目标是使用表单登录方法对用户进行身份验证. 2.认证和授权(Authe ...

  7. Spring MVC 数据验证——validate注解方式

    1.说明 学习注解方式之前,应该先学习一下编码方式的spring注入.这样便于理解验证框架的工作原理.在出错的时候,也能更好的解决这个问题.所以本次博客教程也是基于编码方式.仅仅是在原来的基础加上注解 ...

  8. Spring Security 使用数据库用户进行认证

    本文参考或摘录自:http://haohaoxuexi.iteye.com/blog/2157769 本文使用Spring Security自带的方式连接数据库对用户进行认证. 1.Spring Se ...

  9. SpringBoot Spring Security 核心组件 认证流程 用户权限信息获取详细讲解

    前言 Spring Security 是一个安全框架, 可以简单地认为 Spring Security 是放在用户和 Spring 应用之间的一个安全屏障, 每一个 web 请求都先要经过 Sprin ...

随机推荐

  1. http://blog.csdn.net/v_july_v/article/details/6543438

    本文转载至: http://blog.csdn.net/v_july_v/article/details/6543438 算法 程序员面试.算法研究.编程艺术.红黑树.数据挖掘5大经典原创系列集锦与总 ...

  2. Windows查看网络端口被占用情况netstat命令

    在windows命令行窗口下执行: C:\>netstat -aon|findstr "80" TCP     127.0.0.1:80         0.0.0.0:0  ...

  3. spring定时器的cronexpression表达式

    转自:https://www.cnblogs.com/yaowen/p/3779284.html 相关配置: import com.alibaba.dubbo.config.annotation.Se ...

  4. PAT 1013 Battle Over Cities(并查集)

    1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  5. Python IDLE或shell中切换路径

    在Python自带的编辑器IDLE中或者python shell中不能使用cd命令,那么跳到目标路径呢.方法是使用os包下的相关函数实现路径切换功能. import os  os.getcwd() # ...

  6. IIS7发布wcf服务遇到的问题

    如果没有上图所列出的asp.net 40,请在cmd中运行:C:\Windows\Microsoft.NET\Framework\V4.0.30319\aspnet_regiis -i 注:C:\Wi ...

  7. Java 语言基础之运算符

    使用运算符之后,肯定有返回结果. 六种运算符: 算术运算符 赋值运算符 比较运算符 逻辑运算符 位运算符 三元运算符 1. 算术运算符 加(+), 减(-), 乘(*), 除(/), 取余(%), 自 ...

  8. 替换DOM元素 parent.replaceChild(new, old)

    p.replaceChild(nodeNext, p.children[j]); p.replaceChild(nodePrev, p.children[j + 1]);

  9. 【Spring MVC】spring mvc中相同的url请求返回不同的结果

    在项目中凡是使用Spring MVC这种控制器的,大多都是返回JSON数据对象,或者JSP页面. 但是相同的URL请求如何让他自动的选择放回的是什么? 在这里有由于鄙人没有亲自测试过,就不敢乱贴代码, ...

  10. 在Centos上打Preempt-rt实时补丁(续)

    经过上一篇,发现2.6.31版本的补丁不能正常运行(还未找到原因),现改用2.6.33版本: kernel:linux-2.6.33.9 patch:patch-2.6.33.9-rt 解压并打好补丁 ...