一、pom.xml

<!-- spring security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-acl</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>

pom.xml

二、web.xml

在原本spring的基础上添加

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml,classpath:spring-hibernate.xml,classpath:spring-security.xml</param-value>
</context-param>
<!-- SpringSecurity filter -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

web.xml

classpath:maven项目中放在src/main/resources下

三、spring-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd"> <!-- 当指定一个http元素的security属性为none时,表示其对应pattern的filter链为空 -->
<http security="none" pattern="/login.jsp"></http>
<http auto-config="true">
<form-login login-page="/login.jsp" default-target-url="/hello.jsp"
login-processing-url="/login.do" authentication-failure-url="/error.jsp"/>
<logout logout-success-url="/login.jsp" />
<access-denied-handler error-page="/error.jsp"/> <intercept-url pattern="/login.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY" />
<intercept-url pattern="/error.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY" />
<intercept-url pattern="/**" access="ROLE_USER" />
</http> <!-- 用于认证的AuthenticationManager -->
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userDetailsService" />
</authentication-manager> <beans:bean id="userDetailsService" class="com.shi.core.service.UserDetailsServiceImpl"></beans:bean> </beans:beans>

spring-security.xml

login-page:自定义登录页面是通过login-page属性来指定的。

login-processing-url:表示登录时提交的地址,默认是“/j-spring-security-check”。这个只是Spring Security用来标记登录页面使用的提交地址,真正关于登录这个请求是不需要用户自己处理的。

default-target-url:通过指定form-login元素的default-target-url属性,我们可以让用户在直接登录后跳转到指定的页面。如果想让用户不管是直接请求登录页面,还是通过Spring Security引导过来的,登录之后都跳转到指定的页面,我们可以通过指定form-login元素的always-use-default-target属性为true来达到这一效果。

authentication-failure-url:认证失败时跳转的页面

error-page:登录失败时跳转的页面

logout-success-url:登陆成功后默认跳转页面

跳过登陆验证可以配置access="IS_AUTHENTICATED_ANONYMOUSLY"来实现

四、UserDetailService.java

@Transactional(readOnly = true)
public class UserDetailsServiceImpl implements UserDetailsService { @Autowired
private UserManager userManager; @Override
public UserDetails loadUserByUsername(String username)throws UsernameNotFoundException { User user = userManager.findUserByLoginName(username); if (user == null) {
throw new UsernameNotFoundException("用户" + username + " 不存在");
} // 获得用户所有角色权限
Set<SimpleGrantedAuthority> grantedAuths = obtainGrantedAuthorities(user); // 初始化登录用户信息
OperatorDetails userDetails = new OperatorDetails(user.getName(), user.getPassword(),
true, true, true, true,
grantedAuths); return userDetails;
} /**
* 获得用户所有角色的权限.
*/
private Set<SimpleGrantedAuthority> obtainGrantedAuthorities(User user) {
Set<SimpleGrantedAuthority> authSet = new HashSet<SimpleGrantedAuthority>();
for (Role role : user.getRoleList()) {
authSet.add(new SimpleGrantedAuthority(role.getRole()));
}
return authSet;
} }

UserDetailsServiceImpl.java

SimpleGrantedAuthority中传String参数   例如ROLE_USER  ROLE_ADMIN

Spring Security (一)的更多相关文章

  1. Spring Security OAuth2 开发指南

    官方原文:http://projects.spring.io/spring-security-oauth/docs/oauth2.html 翻译及修改补充:Alex Liao. 转载请注明来源:htt ...

  2. spring mvc 和spring security配置 web.xml设置

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmln ...

  3. SPRING SECURITY JAVA配置:Web Security

    在前一篇,我已经介绍了Spring Security Java配置,也概括的介绍了一下这个项目方方面面.在这篇文章中,我们来看一看一个简单的基于web security配置的例子.之后我们再来作更多的 ...

  4. 【OAuth2.0】Spring Security OAuth2.0篇之初识

    不吐不快 因为项目需求开始接触OAuth2.0授权协议.断断续续接触了有两周左右的时间.不得不吐槽的,依然是自己的学习习惯问题,总是着急想了解一切,习惯性地钻牛角尖去理解小的细节,而不是从宏观上去掌握 ...

  5. spring security oauth2.0 实现

    oauth应该属于security的一部分.关于oauth的的相关知识可以查看阮一峰的文章:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html ...

  6. Spring Security(08)——intercept-url配置

    http://elim.iteye.com/blog/2161056 Spring Security(08)--intercept-url配置 博客分类: spring Security Spring ...

  7. Spring Security控制权限

    Spring Security控制权限 1,配置过滤器 为了在项目中使用Spring Security控制权限,首先要在web.xml中配置过滤器,这样我们就可以控制对这个项目的每个请求了. < ...

  8. Spring Security笔记:Hello World

    本文演示了Spring Security的最最基本用法,二个页面(或理解成二个url),一个需要登录认证后才能访问(比如:../admin/),一个可匿名访问(比如:../welcome) 注:以下内 ...

  9. Spring Security笔记:自定义Login/Logout Filter、AuthenticationProvider、AuthenticationToken

    在前面的学习中,配置文件中的<http>...</http>都是采用的auto-config="true"这种自动配置模式,根据Spring Securit ...

  10. spring session 和 spring security整合

    背景: 我要做的系统前面放置zuul. 使用自己公司提供的单点登录服务.后面的业务应用也是spring boot支撑的rest服务. 目标: 使用spring security管理权限包括权限.用户请 ...

随机推荐

  1. Adroid 展开收起效果实现

    Layout <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...

  2. 进fastreboot

    1.红米1s 关机后,开机键+音量键下 同时按 2.红米note 关机后,开机键+音量键上 同时按 3. 4. 5.

  3. 建立eureka服务和客户端(客户端获取已经注册服务)

    1. 新建sping boot eureka server 新建立spring starter  project 修改pom.xml文件 在parent后追加 <dependencyManage ...

  4. Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 读取关系数据

    Reading related data¶ 9 of 9 people found this helpful The Contoso University sample web application ...

  5. lua特性纪要

    [局部变量] lua的局部变量通过local进行显示声明, 其作用域仅限于声明它的块block.这里的block分为三种类型: 1.控制结构的执行体 2.函数的执行体 3.chunk 比较容易引起混淆 ...

  6. wireshark常用命令

    Wireshark 基本语法,基本使用方法,及包过虑规则: 1.过滤IP,如来源IP或者目标IP等于某个IP   例子: ip.src eq 192.168.1.107 or ip.dst eq 19 ...

  7. ireport5.6+jasperreport6.3开发(一)--中文环境配置在

    ireport在pdf的情况下无法显示中文字的解决方法 1,首先下载宋体的ttf(注意ttc的不行)下载链接如下(注意你可以用其他的ttf不一定要宋体) http://files.cnblogs.co ...

  8. struts中Cookie实现记住密码

    HttpServletRequest request = ServletActionContext.getRequest(); Cookie[] cookies = request.getCookie ...

  9. wmware 怎么 跟主机相互通信

    VMnet1和VMware8其实就是软件模拟出来的两块网卡提供DHCP服务,两块网卡对应VMware的两种不同的模式VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转 ...

  10. 为了防止采集,把文章中出现的URL链接随机大小写(PHP实现)

    <?php $string = "http://www.kxblogs.com/n/20161115/74439155.html"; $string = explode('/ ...