King


  • Home

  • Archives

  • Tags

springSecurity登录验证

Posted on 2018-05-31 | In springSecurity登录验证

1、配置是在WebSecurityConfigurerAdapter中的configure(HttpSecurity http)方法中提供一个默认的配置

1
2
3
4
5
6
7
8
9
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.httpBasic();

}

上面的默认配置:

  • 确保我们应用所有请求都需要用户被认证
  • 允许用户进行基于表单的认证
  • 允许用户使用http基本验证进行认证

与xml命名配置对比

1
2
3
4
5
<http>
<intercept-url pattern = "/**" access = "authenticated" />
<form-login />
<http-basic />
</http>

在java中的and()方法相当于xml的标签关闭

2、java配置和表单登录

配置类中需要更新前面配置

1
2
3
4
5
6
7
8
9
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login") //*1
.permitALl(); //*2

}

  1. 指定登录页的路径
  2. 允许表单登录的所有用户访问登录界面

页面
我们需要创建一个表单,action跳转登录接口。记住loginPage里面配置是我们跳转登录界面,需要的只是get请求。而页面配置的action跳转的接口,是post请求,只要跟登录接口保持一致即可
登录参数和密码如果你没有配置的话。 需要命名为username和password


3、验证请求

当我们需要进行身份验证并且在应用程序的每个url这样做,我们可以通过给http.authorizeRequests()添加多个及诶单来指定多个定制需求到url中

1
2
3
4
5
6
7
8
9
10
11
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests() //1
.antMatchers("/resources/**", "/signup", "/about").permitAll() //2
.antMatchers("/admin/**").hasRole("ADMIN") //3
.antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')") 4
.anyRequest().authenticated() //5
.and()
// ...
.formLogin();
}

  • http.authorizeRequests()天剑antMatchers多个节点
  • 指定多个url,可以让任何用户都可以访问的url,以resources开头的,还有”/signup”, “/about”
  • admin开头的url只能拥有roleAdmin角色的用户进行访问
  • 任何以db开头的需要同时具备admin 和dba角色才能访问
  • 尚未匹配的任何 URL 要求用户进行身份验证

4、处理登出

当使用WebSecurityConfigurerAdapter时 注销功能会被自动用上,默认是通过访问/logout,这个操作指定一下动作:

  • 使session失效
  • 清除所有已经配置的rememberMe认证
  • 清除SecurityContextHolder
  • 跳转到login?success
    当然你也可以定制注销功能
1
2
3
4
5
6
7
8
9
10
11
12
protected void configure(HttpSecurity http) throws Exception {
http
.logout() // 1
.logoutUrl("/my/logout")// 2
.logoutSuccessUrl("/my/index") // 3
.logoutSuccessHandler(logoutSuccessHandler)//4
.invalidateHttpSession(true) // 5
.addLogoutHandler(logoutHandler) // 6
.deleteCookies(cookieNamesToClear) //7
.and()
...
}
  1. 提供注销支持,使WebSecurityConfigurerAdapter是会自动被应用
  2. 设置触发注销操作的url,如果csrf保护被启动(默认启动)的话这个请求的方式被限定为post请求
  3. 注销之后跳转的url,默认是/login?logout
  4. 定制logoutSuccessHandler,如果指定了,那么logoutSuccessUrl()的设置会被忽略
  5. 指定是否在注销时让httpSession无效,默认true
  6. 添加一个logoutHandler,默认是SecurityContextLogoutHandler,会被添加为最后一个 logouthandler
  7. 允许指定注销成功时将移除cookie

LogoutHandler

流式API提供了一个调用相应的LogoutHandler实现的快捷方式,而不用直接提供LogoutHandler的实现
例如:deleteCookies()允许指定注销成功时要删除的一个或者多个cookie。不应该抛出异常

LogoutSuccessHandler

LogoutSuccessHandler 被LogoutFiler在成功注销后调用,用来进行重定向或者转发相应的目的地。可以抛出异常

5、验证

内存中的身份验证

配置多个用户的例子

1
2
3
4
5
6
7
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER").and()
.withUser("admin").password("password").roles("USER", "ADMIN");
}

JDBC验证

下面的例子假设你已经在应用程序中定义好了DateSource , jdbc-jc示例提供了一个完整的基于JDBC的验证

1
2
3
4
5
6
7
8
9
10
11
12
@Autowired
private DataSource dataSource;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.jdbcAuthentication()
.dataSource(dataSource)
.withDefaultSchema()
.withUser("user").password("password").roles("USER").and()
.withUser("admin").password("password").roles("USER", "ADMIN");
}

6、多个HttpSecurity

可以配置多个HttpSecurity实例,多WebSecurityConfigurationAdapter进行多次扩展

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@EnableWebSecurity
public class MultiHttpSecurityConfig {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) { //1
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER").and()
.withUser("admin").password("password").roles("USER", "ADMIN");
}

@Configuration
@Order(1) // 2
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/api/**") // 3
.authorizeRequests()
.anyRequest().hasRole("ADMIN")
.and()
.httpBasic();
}
}

@Configuration // 4
public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin();
}
}
}
  1. 配置正常的验证
  2. 创建一个WebSecurityConfigurerAdapter,包含一个@Order注解,用来指定哪个优先
  3. http.antMatcher指出,这个HttpSecurity只应用的到以/api开头的url上
  4. 创建另外一个WebSecurityConfigurerAdapter实例,用于不以/api开头的url,这个顺讯会在ApiWebSecurityConfigurationAdapter之后,因为没有指定@Order值

7、方法安全

EnableGlobalMethodSecurity

我们可以在任何使用@Configuration的实例上是哟好难过@EnableGlobalMethodSecurity注解来启用基于注解的安全性

1
2
3
4
@EnableGlobalMethodSecurity(securedEnabled = true)
public class MethodSecurityConfig {
// ...
}

添加一个注解到一个方法会限制对相应方法的访问
Spring Security的原生注解支持定义一套用户该方法的属性,这些将被传递到AccessDecisionManager来做实际决定

1
2
3
4
5
6
7
8
9
10
11
public interface BankService {

@Secured("IS_AUTHENTICATED_ANONYMOUSLY")
public Account readAccount(Long id);

@Secured("IS_AUTHENTICATED_ANONYMOUSLY")
public Account[] findAccounts();

@Secured("ROLE_TELLER")
public Account post(Account account, double amount);
}

使用如下代码启用JSR-250注解的支持

1
2
3
4
@EnableGlobalMethodSecurity(jsr250Enabled = true)
public class MethodSecurityConfig {
// ...
}

这些都是基本标准,并允许应用简单的基于角色的约束,但是没有spring Security的本地注解的能力。要使用新的基本表达式的语法,可以使用

1
2
3
4
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig {
// ...
}

和响应java代码

1
2
3
4
5
6
7
8
9
10
11
public interface BankService {

@PreAuthorize("isAnonymous()")
public Account readAccount(Long id);

@PreAuthorize("isAnonymous()")
public Account[] findAccounts();

@PreAuthorize("hasAuthority('ROLE_TELLER')")
public Account post(Account account, double amount);
}

GlobalMethodSecurityConfiguration

有时候需要制定一些比@EnableGlobalMethodSecurity 注解允许的更复杂的操作,我们可以扩展GlobalMethodSecurityConfiguration确保@EnableGlobalMethodSecurity注解出现在你的 子类中

1
2
3
4
5
6
7
8
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
@Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
// ... create and return custom MethodSecurityExpressionHandler ...
return expressionHander;
}
}

已配置对象的后续处理

Spring Security的Java配置没有公开每个配置对象的每一个属性,这简化了广大用户的配置。毕竟如果要配置每一个属性,用户可以使用标准的Bean配置。

虽然有一些很好的理由不直接暴露所有属性,用户可能任然需要更多高级配置,为了解决这个Spring Security引入了 ObjectPostProcessor 概念,用来修改或替换Java配置的对象实例。例如:如果你想在FilterSecurityInterceptor里配置filterSecurityPublishAuthorizationSuccess属性,你可以像下面一样:

1
2
3
4
5
6
7
8
9
10
11
12
13
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
public <O extends FilterSecurityInterceptor> O postProcess(
O fsi) {
fsi.setPublishAuthorizationSuccess(true);
return fsi;
}
});
}

git日常用法

Posted on 2017-08-14 | In git日常用法

git日常用法

  1. 去自己的工作分支
    $ git checkout work

  2. 工作
    ….

  3. 提交工作分支的修改
    $ git commit -a

  4. 回到主分支
    $ git checkout master

  5. 获取远程最新的修改,此时不会产生冲突
    $ git pull

  6. 回到工作分支
    $ git checkout work

  7. 用rebase合并主干的修改,如果有冲突在此时解决
    $ git rebase master

  8. 回到主分支
    $ git checkout master

  9. 合并工作分支的修改,此时不会产生冲突。
    $ git merge work

  10. 提交到远程主干
    $ git push

Untitled

Posted on 2017-06-17

SqlServer发布订阅

Posted on 2017-05-12 | In SqlServer数据库

SqlServer发布订阅

  1. 环境准备
    需要准备两台服务器、创建两个数据库advocate、follow
    需要在另外一台服务器上,以机器名进行登录访问数据库,需要将数据库中方面–》服务器配置–》RemoteDacEnabled修改为false
  2. 在advocate数据库中点击复制–>右击本地发布新建发布
    "新建发布"
  3. 下一步选择自己所要同步的数据库
    "新建发布"
  4. 选择发布类型,这里我先使用快照发布(每个发布类型说明对应下面有)
    "新建发布"
  5. 下一步,选择需要同步的表或者视图
    "新建发布"
  6. 下一步第一张图是选择同步视图的时候的问题、下面是自己需要筛选的表数据,当然不需要则可以继续下一步,需要则点添加出现后面填写语句地方
    "新建发布"
    "新建发布"
    "新建发布"
  7. 继续下一步。勾选下面两个选项,并自己设定下计划、需要多久同步一次数据
    "新建发布"
    "新建发布"
  8. 下一步,安全设置,使用实际需要的数据库登录密码
    "新建发布"
    "新建发布"
  9. 继续,将发布的名字设定一下
    "新建发布"
    "新建发布"
    "新建发布"
    "新建发布"
    接下来对follow数据进行订阅操作
  10. 创建一个订阅,需要选择创建发布的服务器
    "新建发布"
    "新建发布"
    "新建发布"
    点击浏览更多
    "新建发布"
    选择网络服务器
    "新建发布"
  11. 选择下一步,选择在分发服务器上运行所有代理
    当需要选择在 “在其订阅服务器上运行每个代理(请求订阅)”,需要在两台服务器上,创建一个系统用户,用户名和密码弄同样。
    在启动服务,查看SqlServer代理,属性进行设置使用刚刚创建的用户名进行运行。

"新建发布"

  1. 选择需要订阅的数据库
    "新建发布"
  2. 设置服务器的连接
    "新建发布"
    "新建发布"
  3. 然后一直到完成
    "新建发布"
    "新建发布"
    "新建发布"
    "新建发布"
    第一次熟悉SqlServer的订阅发布进行数据库的同步,后续还需要继续完善测试性能问题,还有中间遇到的一些错误

发布成功后
再对应订阅服务器中,右键订阅,选择属性,选择快照位置,文件夹填写发布服务器上面快照存储的文件夹,(发布服务器快照文件夹需要配置共享)
"新建发布"

SQL SERVER 2005中的同步复制技术
当需要进行服务器名称进行访问内网另一台时,需要修改host文件,新加一行
IP地址 服务器名

一、准备工作:

  1. 建立一个 WINDOWS 用户,设置为管理员权限,并设置密码,作为发布快照文件的有效访问用户。

  2. 在SQL SERVER下实现发布服务器和订阅服务器的通信正常(即可以互访)。打开1433端口,在防火墙中设特例

  3. 在发布服务器上建立一个共享目录,作为发布快照文件的存放目录。例如:在D盘根目录下建文件夹名为SqlCopy

  4. 设置SQL 代理(发布服务器和订阅服务器均设置)

打开服务(控制面板—管理工具—服务)

—右击SQLSERVER AGENT—属性—登录—选择“此帐户“

—输入或选择第一步中创建的WINDOWS 用户

—“密码“中输入该用户密码

  1. 设置SQL SERVER 身份验证,解决连接时的权限问题(发布、订阅服务器均设置)

步骤为:对象资源管理器—-右击SQL实例—–属性—-安全性—-服务器身份验证——选“SQL Server和WINDOWS“,然后点确定

  1. 开启SQL Server 2005的网络协议TCP/IP和管道命名协议并重启网络服务。

  2. 在SQL Server中创建步骤1中对应的系统用户登陆名,作为发布数据库的拥有者(设置为dbo_owner和public)。

  3. 以系统超级用户sa登陆SQL Server建立数据库和表。

  4. 发布服务器和订阅服务器互相注册

步骤如下:视图—-单击以注册服务器—-右键数据库引擎—-新建服务器注册—–填写要注册的远程服务器名称——身份验证选“SQL Server验证“—–用户名(sa) 密码——创建组(也可不建)—–完成。

  1. 对于只能用IP,不能用计算机名的,为其注册服务器别名

二、开始:

发布服务器配置(在发布服务器上配置发布和订阅)

  1. 选择 复制 节点

  2. 右键本地发布 —-下一步———系统弹出对话框看提示—-直到“指定快照文件夹“

—-在“快照文件夹“中输入准备工作中创建的目录(指向步骤3所建的共享文件夹)——选择发布数据库——-选择发布类型——-选择订阅服务器类型——-选择要发布的对象——设置快照代理——-填写发布名称。

  1. 右键本地订阅——–选择发布服务器——-选择订阅方式(如果是在服务器方订阅的话选择推送订阅反之选择请求订阅)——-填加订阅服务器——–选择代理计划(一般选择连续运行)———其余选择默认项。

至此, SQL SERVER 2005 同步复制就完成了。使用复制技术,用户可以将一份客户端的数据发布到多台服务器上,从而使不同的服务器用户都可以在权限的许可的范围内共享这份数据。复制技术可以确保分布在不同地点的数据自动同步更新,从而保证数据的一致性,就无需编程实现客户端和服务器端数据同步了!大大提高了工作效率!

Hello World

Posted on 2017-05-09

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

Liudade

Liudade

5 posts
3 categories
6 tags
© 2018 Liudade
Powered by Hexo
Theme - NexT.Muse