Showing posts with label plugin. Show all posts
Showing posts with label plugin. Show all posts

2013-11-05

Android Ajax Authentication using Android-Query and Grails Spring Security Plugin

This post shows code on client end only.

Code in fragment class:
private AQuery aq;
String svalue = null; // keep current session id

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
  Bundle savedInstanceState) {
  View rootView = inflater.inflate(R.layout.fragment_main, container, false);
  aq = new AQuery(getActivity(), rootView);

  aq.id(R.id.loginbutton).clicked(this, "onLoginButtonClick");
  aq.id(R.id.logoutbutton).clicked(this, "onLogoutButtonClick");
  aq.id(R.id.dobutton).clicked(this, "onDoButtonClick");
  return rootView;
}


Login:
public void onLoginButtonClick(View view) {
  String url = "http://10.0.3.2:8090/GrailsTest1/j_spring_security_check";
  Map<string object=""> params = new HashMap<string object="">();
  params.put("j_username", "admin");
  params.put("j_password", "admin");
  params.put("ajax", "true");

  cb = new AjaxCallback<jsonobject>();
  cb.url(url).type(JSONObject.class).params(params).weakHandler(this, "loginCallback");
  cb.header("Content-Type", "application/x-www-form-urlencoded");
  aq.ajax(cb);
}

public void loginCallback(String url, JSONObject json, AjaxStatus status) throws JSONException {
  if(json != null) {
    List<cookie> cookieList = new ArrayList<cookie>();
    cookieList = status.getCookies();
    for (Cookie cookie : cookieList) {
    if(cookie.getName().equals("JSESSIONID")) {
      svalue = cookie.getValue();
    } else {
      svalue = null;
    }
    
    // do something when succeed to log in
  } else {
    // do something when fail to log in
  }
}


Logout:
public void onButton3Click(View view) {
  String url = "http://10.0.3.2:8090/GrailsTest1/logout/index?ajax=true";
  Map<string object=""> params = new HashMap<string object="">();
  AjaxCallback<string> cb = new AjaxCallback<string>();
  if(svalue != null) cb.cookie("JSESSIONID", svalue);
  cb.url(url).type(JSONObject.class).params(params).weakHandler(this, "logoutCallback");
  aq.ajax(cb);
}

public void logoutCallback(String url, String json, AjaxStatus status) throws JSONException {
  if(json != null){
    // do something when succeed to log out
  }else{
    // do something when fail to log out
  }
}


Do something need to run with or whithout authentication:
public void onDoButtonClick(View view) {
  String url = "http://10.0.3.2:8090/GrailsTest1/main/doAction?ajax=true";
  AjaxCallback<string> cb = new AjaxCallback<string>();
  cb.url(url).type(JSONObject.class).weakHandler(this, "jsonCallback");
  if(svalue != null) cb.cookie("JSESSIONID", svalue);
  aq.ajax(cb);
}

public void jsonCallback(String url, JSONObject json, AjaxStatus status) throws JSONException {
  if(json != null){
    // do something when succeed to get data from server 
  }else{
    // do something when fail to get data from server
  }
}

2013-10-31

Where's the file LogoutController.grooxy in project after installed Grails Spring Security 2.0RC plugin

Sometimes, we need override class LoginController or LogoutController when using ajax authentication function of Grails Spring Security plugin. But in 2.0RC version, after you run s2-quickstart, the LoginController and LogoutController class are not created in the grails-app\controllers folder as before. where're they now? Oh, they're hidden deeply:

\target\work\plugins\spring-security-core-2.0-RC2\grails-app\controllers\grails\plugin\springsecurity

if you still can not find it, please use file search function in the IDE.

Remember copying the file to grails-app\controllers folder before you using and override it.

get error when use logout in the page genarated by Grails Spring Security Pluin

the error message is:

    The specified HTTP method is not allowed for the requested resource.


reason:
Grails Spring Security Plugin use GET request to logout, but its config set default logout request methord to POST for safety reason.

solution:
in Config.groovy, add:
grails.plugin.springsecurity.logout.postOnly = false

or, just use POST request to logout in your application, that's more safe.


@Secured annotation can not be resolved in Grails 2.3.1

error message in editor:

    Secured is not an annotation in @Secured

solution:
add:
import grails.plugin.springsecurity.annotation.Secured

2013-10-30

How to install Spring Security Core Plugin 2.0 in Grails


1. in BuildConfig.groovy, add:
compile ':spring-security-core:2.0-RC2'
and maven repository:
mavenRepo 'http://repo.spring.io/milestone'
    note: if not set this maven repository, you will get error:

    org.springframework.security#spring-security-core;3.2.0.RC1: not found

2. run
$ grails compile
    note: if skip this step you will get error:

    Error org.codehaus.groovy.grails.cli.ScriptNotFoundException

3. then run
$ grails s2-quickstart com.testapp User Role
4. done.