2014年6月4日 星期三

[Shiro] url get multiple roles

在spring-security中,如果要對網址控管多個角色可進入
一般會透過roles[admin]
ex: /admin/** = user, roles[admin] 

但是 roles["admin,guest"] ,會被認定要符合這些角色(hasAllRoles)
因此,必須自己去定義,改寫RolesAuthorizationFilter就可以
參考這篇
http://blog.abelsky.com/2014/01/27/make-apache-shiro-allow-several-roles-to-access-resource/

public class AnyOfRolesAuthorizationFilter extends RolesAuthorizationFilter {
 
    @Override
    public boolean isAccessAllowed(ServletRequest request, ServletResponse response,
                                   Object mappedValue) throws IOException {
 
        final Subject subject = getSubject(request, response);
        final String[] rolesArray = (String[]) mappedValue;
 
        if (rolesArray == null || rolesArray.length == 0) {
            //no roles specified, so nothing to check - allow access.
            return true;
        }
 
        for (String roleName : rolesArray) {
            if (subject.hasRole(roleName)) {
                return true;
            }
        }
 
        return false;
    }
}

最後在spring-security.xml中加入
<bean id="anyOfRoles" class="com.your.package.AnyOfRolesAuthorizationFilter" />
就可以透過anyOfRoles去使用多個角色
/path/to/some/url = anyofroles["role1,role2"]

Other reference:
http://shiro-user.582556.n2.nabble.com/Shiro-ini-multiple-roles-for-one-url-td6806837.html

2014年5月19日 星期一

[SQL] get sysdate to millisecond


利用SYSTIMESTAMP加上TO_CHAR()給格式,可以取到毫秒


ParameterExplanation
YEARYear, spelled out
YYYY4-digit year
YYY
YY
Y
Last 3, 2, or 1 digit(s) of year.
IYY
IY
I
Last 3, 2, or 1 digit(s) of ISO year.
IYYY4-digit year based on the ISO standard
QQuarter of year (1, 2, 3, 4; JAN-MAR = 1).
MMMonth (01-12; JAN = 01).
MONAbbreviated name of month.
MONTHName of month, padded with blanks to length of 9 characters.
RMRoman numeral month (I-XII; JAN = I).
WWWeek of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year.
WWeek of month (1-5) where week 1 starts on the first day of the month and ends on the seventh.
IWWeek of year (1-52 or 1-53) based on the ISO standard.
DDay of week (1-7).
DAYName of day.
DDDay of month (1-31).
DDDDay of year (1-366).
DYAbbreviated name of day.
JJulian day; the number of days since January 1, 4712 BC.
HHHour of day (1-12).
HH12Hour of day (1-12).
HH24Hour of day (0-23).
MIMinute (0-59).
SSSecond (0-59).
SSSSSSeconds past midnight (0-86399).
FFFractional seconds.

Reference:
http://www.oracle.com/technetwork/issue-archive/2012/12-jan/o12plsql-1408561.html
http://www.techonthenet.com/oracle/functions/to_char.php

2014年2月12日 星期三

[SQL]multiple rows into a single column



參考:https://community.oracle.com/message/3788432
select deptno,
       rtrim (xmlagg (xmlelement (e, ename || ',')).extract ('//text()'), ',') enames
from emp
group by deptno

2013年11月18日 星期一

[JAVA]日期比對 CompareTo

Date date1 = sdf.parse("2009-12-31");
Date date2 = sdf.parse("2010-01-31");
 
System.out.println(sdf.format(date1));
System.out.println(sdf.format(date2));
 
if(date1.compareTo(date2)>0){
   System.out.println("Date1 is after Date2");
}else if(date1.compareTo(date2)<0){
   System.out.println("Date1 is before Date2");
}else if(date1.compareTo(date2)==0){
   System.out.println("Date1 is equal to Date2");
}else{
   System.out.println("How to get here?");
}


http://www.mkyong.com/java/how-to-compare-dates-in-java/

2013年7月31日 星期三

[JSTL]判斷該欄位是否為空值

在JSTL裡,判斷欄位是否為空值

用 empty

ex: empty each.remCuser             判斷rmeCuser為空的
      not empty each.remCuser     判斷rmeCuser不為空的

[JAVA]比對Collection的值是否一樣

可以用containsKey去檢查map的key是否一樣
如此就不用一個一個去比對


http://www.roseindia.net/tutorial/java/core/hashMap/containsKey.html

HashMapContainsKey.java
package net.roseindia.java;
import java.util.HashMap;
public class HashMapContainsKey {
public static void main(String[] arr) {
/* Create object of HashMap */
HashMap<Integer, String> obMap = new HashMap<Integer, String>();
/* Add value in HashMap */
obMap.put(new Integer(1), "Bharat");
obMap.put(new Integer(2), "Gyan");
obMap.put(new Integer(4), "Vrishti");
obMap.put(new Integer(3), "Sarika");
obMap.put(new Integer(5), "Rohit");
obMap.put(new Integer(6), "Parineeta");
/* Check key present in HashMap or not */
System.out.println("Returns :" + obMap.containsKey(1));
System.out.println("Returns :" + obMap.containsKey(8)); } }