Symfony按角色访问控制

时间:2017-08-28 08:19:25

标签: symfony security fosuserbundle

我有ROLE_ADMIN并且我想做一个简单的访问列表路径,如果用户未登录,它将发送到登录页面,如果不是/admin o其他他不能去security.yml页面。

我在access_control: - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/, role: IS_AUTHENTICATED_FULLY } - { path: ^/admin, role: ROLE_ADMIN }

中写道
/admin page

但现在所有登录的用户都可以转到 package in.marksys.www.attendancemanagement; import android.annotation.SuppressLint; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.os.Parcelable; import android.provider.MediaStore; import android.webkit.ConsoleMessage; import android.webkit.ValueCallback; import android.webkit.WebBackForwardList; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; import java.io.File; public class MainActivity extends Activity { private WebView webView; private Toast toast; private long lastBackPressTime = 0; //File choser parameters private static final int FILECHOOSER_RESULTCODE = 2888; final Activity activity = this; public Uri imageUri; private ValueCallback<Uri> mUploadMessage; //Camera parameters private Uri mCapturedImageURI = null; @SuppressLint("SetJavaScriptEnabled") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Get webview webView = (WebView) findViewById(R.id.webView); // Define url that will open in webview String webViewUrl = "http://marksystest.in/attendance/applogin.php"; // Javascript inabled on webview webView.getSettings().setJavaScriptEnabled(true); // Other webview options webView.getSettings().setLoadWithOverviewMode(true); //webView.getSettings().setUseWideViewPort(true); //Other webview settings webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); webView.setScrollbarFadingEnabled(false); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setPluginState(WebSettings.PluginState.ON); webView.getSettings().setAllowFileAccess(true); webView.getSettings().setSupportZoom(true); //Load url in webview webView.loadUrl(webViewUrl); // Define Webview manage classes startWebView(); } private void startWebView() { // Create new webview Client to show progress dialog // Called When opening a url or click on link // You can create external class extends with WebViewClient // Taking WebViewClient as inner class webView.setWebViewClient(new WebViewClient() { ProgressDialog progressDialog; //If you will not use this method url links are open in new brower not in webview public boolean shouldOverrideUrlLoading(WebView view, String url) { // Check if Url contains ExternalLinks string in url // then open url in new browser // else all webview links will open in webview browser if(url.contains("google")){ // Could be cleverer and use a regex //Open links in new browser view.getContext().startActivity( new Intent(Intent.ACTION_VIEW, Uri.parse(url))); // Here we can open new activity return true; } else { // Stay within this webview and load url view.loadUrl(url); return true; } } //Show loader on url load public void onLoadResource (WebView view, String url) { // if url contains string androidexample // Then show progress Dialog if (progressDialog == null && url.contains("androidexample") ) { // in standard case YourActivity.this progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setMessage("Loading..."); progressDialog.show(); } } // Called when all page resources loaded public void onPageFinished(WebView view, String url) { try{ // Close progressDialog if (progressDialog.isShowing()) { progressDialog.dismiss(); progressDialog = null; } }catch(Exception exception){ exception.printStackTrace(); } } }); // You can create external class extends with WebChromeClient // Taking WebViewClient as inner class // we will define openFileChooser for select file from camera or sdcard webView.setWebChromeClient(new WebChromeClient() { // openFileChooser for Android 3.0+ public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType){ // Update message mUploadMessage = uploadMsg; try{ // Create AndroidExampleFolder at sdcard File imageStorageDir = new File( Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES) , "AndroidExampleFolder"); if (!imageStorageDir.exists()) { // Create AndroidExampleFolder at sdcard imageStorageDir.mkdirs(); } // Create camera captured image file path and name File file = new File( imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg"); mCapturedImageURI = Uri.fromFile(file); // Camera capture image intent final Intent captureIntent = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE); captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI); Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); // Create file chooser intent Intent chooserIntent = Intent.createChooser(i, "Image Chooser"); // Set camera intent to file chooser chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS , new Parcelable[] { captureIntent }); // On select image call onActivityResult method of activity startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE); } catch(Exception e){ Toast.makeText(getBaseContext(), "Exception:"+e, Toast.LENGTH_LONG).show(); } } // openFileChooser for Android < 3.0 public void openFileChooser(ValueCallback<Uri> uploadMsg){ openFileChooser(uploadMsg, ""); } //openFileChooser for other Android versions public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { openFileChooser(uploadMsg, acceptType); } // The webPage has 2 filechoosers and will send a // console message informing what action to perform, // taking a photo or updating the file public boolean onConsoleMessage(ConsoleMessage cm) { onConsoleMessage(cm.message(), cm.lineNumber(), cm.sourceId()); return true; } public void onConsoleMessage(String message, int lineNumber, String sourceID) { //Log.d("androidruntime", "Show console messages, Used for debugging: " + message); } }); // End setWebChromeClient } // Return here when file selected from camera or from SDcard @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { if(requestCode==FILECHOOSER_RESULTCODE) { if (null == this.mUploadMessage) { return; } Uri result=null; try{ if (resultCode != RESULT_OK) { result = null; } else { // retrieve from the private variable if the intent is null result = intent == null ? mCapturedImageURI : intent.getData(); } } catch(Exception e) { Toast.makeText(getApplicationContext(), "activity :"+e, Toast.LENGTH_LONG).show(); } mUploadMessage.onReceiveValue(result); mUploadMessage = null; } } @Override // Detect when the back button is pressed public void onBackPressed() { if(webView.canGoBack()) { WebBackForwardList mWebBackForwardList = webView.copyBackForwardList(); String historyUrl = mWebBackForwardList.getItemAtIndex(mWebBackForwardList.getCurrentIndex()-1).getUrl(); String loginurl = "http://marksystest.in/attendance/applogin.php"; if(historyUrl==loginurl) { super.onBackPressed(); // toast = Toast.makeText(this, historyUrl, Toast.LENGTH_SHORT); //toast.show(); } else { webView.goBack(); } } else { // Let the system handle the back button super.onBackPressed(); } } } ...

我不明白我的错。

3 个答案:

答案 0 :(得分:4)

规则按照它们编写的顺序进行解析,

- { path: ^/, role: IS_AUTHENTICATED_FULLY }
如果用户已通过身份验证,

将授予对/下所有区域的访问权限

您需要切换最后两条规则,所有规则肯定会按预期工作。

答案 1 :(得分:3)

尝试更改

    - { path: ^/, role: IS_AUTHENTICATED_FULLY }
    - { path: ^/admin, role: ROLE_ADMIN }

    - { path: ^/admin, role: ROLE_ADMIN }
    - { path: ^/, role: IS_AUTHENTICATED_FULLY }

答案 2 :(得分:0)

只需删除

- { path: ^/, role: IS_AUTHENTICATED_FULLY }