用*.config文件配置JAAS的LoginModules
来源:互联网 发布:北大陈晓明 知乎 编辑:程序博客网 时间:2024/06/05 19:11
首先编写一个jaas.config文件,内容视你的*LoginModules而定。
simple { com.boonya.demo.business.security.SimpleLoginModule required debug=true; };
一、错误地认为只要将文件写入属性就可以实现LoginModels的注册
一般地,对于配置属性文件而言并不能实现LoginModules的注册,如下为错误的方式:
java.security.auth.login.config=C:\\Program Files\\jaas.config
这样并不能实现目标文件内容的注册。访问的时候会出现这样的错误:
二、正确的注入方式是设置System.setProperty(k,v)方法
(1)、在properties文件中配置jaas.config的路径:
java_security_file_path=C:\\Program Files\\jaas.config注:创建文件时,必须在一个确定存在的路径下完成。
(2)、创建文件jaas.config文件,并作用户登录验证:
package com.boonya.demo.business.controller;import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.io.IOException;import javax.security.auth.Subject;import javax.security.auth.login.LoginContext;import javax.security.auth.login.LoginException;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import com.boonya.demo.business.security.MyPrivilege;import com.boonya.demo.business.security.SimpleCallbackHandler;import com.boonya.demo.utils.ProcessProperties;@Controller@RequestMapping("/securityservice")public class AuthenticationController{/** * 創建JAAS登錄Model * @return */private boolean createJAASFile(){boolean status = false;File file = new File(ProcessProperties.getContextProperty("java_security_file_path"));if (file.exists()){status = true;} else{try{// 創建的文件必須是已經存在的路徑下file.createNewFile();BufferedWriter output = new BufferedWriter(new FileWriter(file));String content = "simple { com.boonya.demo.business.security.SimpleLoginModule required debug=true; };";output.write(content);output.close();status = true;} catch (IOException e){e.printStackTrace();}}return status;}@SuppressWarnings("unchecked")@RequestMapping(value = "/authenticate", method = RequestMethod.GET)public String authentication(@RequestParam("username")String username,@RequestParam("password")String password){System.out.println("create and valide file。。。。。。。。。");boolean status=createJAASFile();System.out.println("create and valide file。。。。。。。。。"+status);// 建立登陆上下文,并通过配置文件初始化,在这里配置文件必须与程序同目录LoginContext loginContext = null;try{// 創建JAAS文件if (!status){return "error";}// 设置JAAS配置文件System.setProperty("java.security.auth.login.config", ProcessProperties.getContextProperty("java_security_file_path"));loginContext = new LoginContext("simple", new SimpleCallbackHandler(username,password));} catch (LoginException e){System.out.println(e.getMessage());} // 建立登陆上下文,并通过配置文件初始化,在这里配置文件必须与程序同目录try{// 如果不抛出异常表示验证成功loginContext.login();Subject subject = loginContext.getSubject();// subject.dSubject.doAs(subject, new MyPrivilege());} catch (LoginException e){return "error";}return "welcome";}}
此处使用了错误的属性读取方式:
package com.boonya.demo.utils;import java.util.HashMap;import java.util.Map;import java.util.Properties;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;public class ProcessProperties extends PropertyPlaceholderConfigurer{private static Map<String, String> ctxPropertiesMap;@Overrideprotected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException{super.processProperties(beanFactoryToProcess, props);ctxPropertiesMap = new HashMap<String, String>();for (Object key : props.keySet()){String keyStr = key.toString();String value = props.getProperty(keyStr);ctxPropertiesMap.put(keyStr, value);}}public static String getContextProperty(String name){return ctxPropertiesMap.get(name);}}
最方便的是下面所说的快速注入,请继续往下看。
三、快速的注入参数到System属性上
文件配置:
<bean id="propertyConfigurer" class="com.boonya.demo.utils.ProcessProperties"> <property name="ignoreResourceNotFound" value="true" /> <property name="locations"> <list> <value>classpath:config.properties</value> </list> </property> </bean>
实现代码:
package com.boonya.demo.utils;import java.util.HashMap;import java.util.Map;import java.util.Properties;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;public class ProcessProperties extends PropertyPlaceholderConfigurer{@Overrideprotected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException{super.processProperties(beanFactoryToProcess, props);for (Object key : props.keySet()){String keyStr = key.toString();String value = props.getProperty(keyStr);System.setProperty(keyStr, value);}}public static String getContextProperty(String name){return System.getProperty(name);}}
0 0
- 用*.config文件配置JAAS的LoginModules
- web.config文件的配置
- Jetty配置JAAS(独立的Jetty服务器)
- NHibernate config 文件的配置 事例!!
- CONFIG.SYS文件的命令与配置
- web.config文件的配置 作
- web.config文件配置的一些说明
- 网页上传文件 大小的config 配置
- 内核配置config失败需要的文件
- 保存配置好的.config文件
- app.config文件的配置及应用
- springmvc_mybatis1210配置config下所有的文件
- Mybatis-config.xml文件的配置
- .net用代码配置Web.config文件
- Config文件配置面面观
- Web.Config文件配置
- web.config文件配置
- 读写配置config文件
- 薃薇羀蒃葿薇肂芆莅薆膄聿
- 莇薅肇芆蕿薇膃莅蒈羃袆膁
- 羃羂蒆蒂蕿肄芈莈薈膇蒄蚆
- 羅肅薆蒇袈肁莃蝿袀膇蒂蚈
- 蒈羂羈蒈薁螅芆蒇蚃羀膂薆
- 用*.config文件配置JAAS的LoginModules
- 使用awk处理makefile
- iOS GCD常用方法总结
- 树莓派(RASPBERRYPI)安装AIRCRACK-NG,REAVER及WIFI破解教程[整理]
- 19-java5同步集合类
- linux 系统管理 - 用户管理
- UVA 10671 (dp 时间的转化)
- Codeforces Round #297 (Div. 2)(模拟+字符串+排序)
- 直接拿来用!最火的Android开源项目(一)