Shiro's HelloWorld

Source: Internet
Author: User

    • Password file
    • Test
Package Org.zln.hello;import Org.apache.log4j.logmanager;import org.apache.log4j.logger;import Org.apache.shiro.securityutils;import Org.apache.shiro.authc.usernamepasswordtoken;import Org.apache.shiro.config.inisecuritymanagerfactory;import Org.apache.shiro.mgt.securitymanager;import Org.apache.shiro.subject.subject;import org.apache.shiro.util.factory;/** * Created by Sherry on 16/9/8.    */public class HelloWorld {private static Logger Logger = Logmanager.getlogger (Helloworld.class);    private static Subject Subject; static {//1, get SecurityManager factory, use Ini Config file to initialize SecurityManager Factory<SecurityManager>factory = new Inisecuritymanagerfactory ("Classpath:shiro.ini");        2, Get SecurityManager instance and bind to securityutils securitymanager SecurityManager = Factory.getinstance ();        Securityutils.setsecuritymanager (SecurityManager);    3. Get Subject and create username/password Authentication Token (i.e. user identity/credentials) Subject = Securityutils.getsubject (); public static Boolean login (String username,string password) {Usernamepasswordtoken token = new Usernamepasswo        Rdtoken (Username,password);        4, login, that is, authentication try {subject.login (token);            }catch (Exception e) { ("Authentication failed");            Logger.error (E.getmessage (), E);        return false; } return subject.isauthenticated ();//whether successful login} public static void Main (string[] args) {//6, exit s    Ubject.logout (); }}

The default use of Relam is Inirelam

Shiro's HelloWorld

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.